Hello there,

A couple of questions. Is the Club class is the owning side of the
association? Does deleting the club will also delete the user?

From what you're trying to do, I understand that you're trying to make a
bidirectional one-to-many/many-to-one association. So IMHO, it will make
much more sense if the Club is the inverse side of the association. So if
for example, a User is deleted. The associated references of clubs joined by
the user is also deleted.

Modify your codes so it looks like this.

In your User class,

private Set<Club> clubs = new HashSet<Club>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REMOVE}, mappedBy="club")
@org.hibernate.annotations.Cascade(
org.hibernate.annotations.CascadeType.DELETE_ORPHAN) //If you're no using
hibernate delete this portion. This is for any references to the clubs own
by the user is also deleted
public Set<User> getClubs() {
  return clubs;
}

//To be able to do transitive persistence. Add this method...
public void addClubs(Club club) {
       if(club== null)
           throw new IllegalArgumentException("Can't add null object");
      club.setClub(this);
       this.getClubs().add(club);
   }


And for your Club class POJO..
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "clubID", nullable=false, updatable=false)
public User getUsers() {
  return user;
}


Hopes this help.


On 7/5/07, Fan <[EMAIL PROTECTED]> wrote:


Here is what I have in club POJO:

@OneToMany(targetEntity=org.appfuse.model.User.class,
cascade=CascadeType.ALL, mappedBy="club", fetch = FetchType.LAZY)
        public Set<User> getUsers()
        {
                return users;
        }

Here is what I have in User POJO

@ManyToOne(optional=false)
    @JoinColumn(name="clubID", nullable=false, updatable=false)
    public Club getClub()
    {
        return club;
    }

But I still hitting the error:

org.springframework.beans.factory.BeanCreationException: Error creating
bean
with name 'sessionFactory' defined in URL [jar:file:/C:/Documents and
Settings/fan/.m2/repository/org/appfuse/appfuse-hibernate/2.0-m5/appfuse-
hibernate-2.0-m5.jar!/applicationContext-dao.xml]:
Invocation of init method failed; nested exception is
org.hibernate.AnnotationException: mappedBy reference an unknown target
entity property: org.appfuse.model.User.club in
com.smartclub.model.Club.users
Caused by: org.hibernate.AnnotationException: mappedBy reference an
unknown
target entity property: org.appfuse.model.User.club in
com.smartclub.model.Club.users
        at
org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(
CollectionBinder.java:543)
        at
org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(
CollectionBinder.java:508)
        at
org.hibernate.cfg.CollectionSecondPass.doSecondPass(
CollectionSecondPass.java:43)
        at
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
        at
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(
AnnotationConfiguration.java:296)
        at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java
:1283)
        at

org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory
(LocalSessionFactoryBean.java:805)
        at

org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory
(LocalSessionFactoryBean.java:745)
        at

org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet
(AbstractSessionFactoryBean.java:134)
        at

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods
(AbstractAutowireCapableBeanFactory.java:1202)
        at

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean
(AbstractAutowireCapableBeanFactory.java:1172)
        at

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean
(AbstractAutowireCapableBeanFactory.java:428)
        at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(
AbstractBeanFactory.java:251)
        at

org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton
(DefaultSingletonBeanRegistry.java:156)
        at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(
AbstractBeanFactory.java:248)
        at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(
AbstractBeanFactory.java:160)
        at

org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons
(DefaultListableBeanFactory.java:284)
        at
org.springframework.context.support.AbstractApplicationContext.refresh(
AbstractApplicationContext.java:352)
        at

org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext
(AbstractSingleSpringContextTests.java:199)
        at

org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations
(AbstractSingleSpringContextTests.java:179)
        at
org.springframework.test.AbstractSingleSpringContextTests.loadContext(
AbstractSingleSpringContextTests.java:158)
        at
org.springframework.test.AbstractSpringContextTests.getContext(
AbstractSpringContextTests.java:105)
        at
org.springframework.test.AbstractSingleSpringContextTests.setUp(
AbstractSingleSpringContextTests.java:87)
        at junit.framework.TestCase.runBare(TestCase.java:128)
        at
org.springframework.test.ConditionalTestCase.runBare(
ConditionalTestCase.java:69)
        at junit.framework.TestResult$1.protect(TestResult.java:106)
        at junit.framework.TestResult.runProtected(TestResult.java:124)
        at junit.framework.TestResult.run(TestResult.java:109)
        at junit.framework.TestCase.run(TestCase.java:120)
        at junit.framework.TestSuite.runTest(TestSuite.java:230)
        at junit.framework.TestSuite.run(TestSuite.java:225)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at
org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java
:210)
        at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(
AbstractDirectoryTestSuite.java:135)
        at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(
AbstractDirectoryTestSuite.java:122)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:129)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(
SurefireBooter.java:225)
        at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java
:747)



ros wrote:
>
> Here is refference about JPA annotation
>
>
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#OneToMany
>
> here is example:
>
> @Entity
> @Table(name="app_user")
> public class User extends BaseObject implements Serializable,
UserDetails
> {
> ...
>     protected Set<Entity> entitys = new HashSet<Entity>();
> ...
>     @OneToMany(targetEntity=Entity.class, cascade=CascadeType.ALL,
> mappedBy="user", fetch = FetchType.LAZY)
>     public Set<Entity> getEntitys() {
>         return entitys;
>     }
> ...
>     public void setEntitys(Set<Entity> entitys) {
>         this.entitys = entitys;
>     }
> ...
> }
>
> @Entity
> @Table(name="entity")
> public class Entity extends BaseObject implements Comparable,
> Serializable,
>               Cloneable {
> ..
>       protected User user;
> ..
>     @ManyToOne(optional=false)
>     @JoinColumn(name="user_id", nullable=false, updatable=false)
>     public User getUser() {
>         return user;
>     }
> ...
>     public void setUser(User user) {
>         this.user = user;
>     }
> ...
>     //hashCode and  equils functions
> ...
> }
>
> Hope this helps.
>
>
>
> Fan wrote:
>>
>> what's the use of persistence.xml ??
>>
>> I saw only two following entries in the file, should I add a new entry
>> for club pojo ?
>>
>> <class>org.appfuse.model.User</class>
>> <class>org.appfuse.model.Role</class>
>>
>> I did try the following way:
>>
>> In my club POJO, I have
>>
>> @OneToMany
>> public Set<User> getUsers()
>> {
>>   return users;
>> }
>>
>> in my user POJO, I have:
>>
>> @ManyToOne
>> @JoinColumn(name="clubID")
>> public Club getClub()
>> {
>>     return club;
>> }
>>
>> so the result is:
>>
>> 1) I have one column called "clubID" in user table
>> 2) A table called "club_app_user" with two keys "Club_clubID",
"users_id"
>> generated
>>
>> but when I have saved the user record with "clubID" being saved
>> successfully in user table, nothing did happen in "club_app_user" , in
>> other words, the "club_app_user" still empty record, or do I need to
save
>> the record in "club_app_user" explicitly ?
>>
>>
>> ros wrote:
>>>
>>> Fan,
>>>
>>> About 1) in pom.xml there should be
>>>
>>>         <dependency>
>>>             <groupId>org.appfuse</groupId>
>>>             <artifactId>appfuse-${web.framework}</artifactId>
>>>             <version>${appfuse.version}</version>
>>>             <type>warpath</type>
>>>             <exclusions>
>>>                 <!-- This exclusion and the dependency following this
>>> one allow DAO framework switching. -->
>>>                 <!-- You only need these if you want to use JPA or
>>> iBATIS. See APF-565 for more information. -->
>>>                 <!-- It does no harm to leave it in for Hibernate, but
>>> it's not needed. -->
>>>                 <exclusion>
>>>                     <groupId>org.appfuse</groupId>
>>>                     <artifactId>appfuse-hibernate</artifactId>
>>>                 </exclusion>
>>>             </exclusions>
>>>         </dependency>
>>>         <dependency>
>>>             <groupId>org.appfuse</groupId>
>>>             <artifactId>appfuse-${dao.framework}</artifactId>
>>>             <version>${appfuse.version}</version>
>>>             <exclusions>
>>>                 <exclusion>
>>>                     <groupId>org.appfuse</groupId>
>>>                     <artifactId>appfuse-data-common</artifactId>
>>>                 </exclusion>
>>>             </exclusions>
>>>         </dependency>
>>>
>>> Check  / src  / main  / resources  / META-INF  / persistence.xml
>>>
>>>
>>> Fan wrote:
>>>>
>>>> Hey ros:
>>>>
>>>> 1)The original pom.xml already has the exclusion :
>>>>
>>>> <dependency>
>>>>             <groupId>org.appfuse</groupId>
>>>>             <artifactId>appfuse-${web.framework}</artifactId>
>>>>             <version>${appfuse.version}</version>
>>>>             <type>warpath</type>
>>>>             <!-- This exclusion and the dependency following this one
>>>> allow DAO framework switching. -->
>>>>             <!-- You only need these if you want to use JPA or
iBATIS.
>>>> See APF-565 for more information. -->
>>>>             <!-- It does no harm to leave it in for Hibernate, but
it's
>>>> not needed. -->
>>>>             <exclusions>
>>>>                 <exclusion>
>>>>                     <groupId>org.appfuse</groupId>
>>>>                     <artifactId>appfuse-hibernate</artifactId>
>>>>                 </exclusion>
>>>>             </exclusions>
>>>> </dependency>
>>>>
>>>> 2) what did you mean by "Then add in to your poroject User and Role
>>>> pojos at appfuse namespace" ?
>>>>
>>>> 3) I did check the user pojo, it does has the foreign key club_clubID
>>>>
>>>> 4) Yup, I am using M5
>>>>
>>>> 5) I did include the bean definition in applicationContext.xml
>>>>
>>>> <bean class="org.appfuse.dao.spring.HibernateExtensionPostProcessor">
>>>>         <property name="annotatedClasses">
>>>>             <list>
>>>>                 <value>com.smartclub.model.Club</value>
>>>>                 <value>com.smartclub.model.Facility</value>
>>>>             </list>
>>>>         </property>
>>>> </bean>
>>>>
>>>> 6) I did include the mapping in hibernate.cfg.xml as well
>>>>
>>>> <hibernate-configuration>
>>>>     <session-factory>
>>>>         <mapping class="org.appfuse.model.User"/>
>>>>         <mapping class="org.appfuse.model.Role"/>
>>>>         <mapping class="com.smartclub.model.Club"/>
>>>>         <mapping class="com.smartclub.model.Facility"/>
>>>>     </session-factory>
>>>> </hibernate-configuration>
>>>>
>>>>
>>>> What else I have to check ??
>>>>
>>>>
>>>> ros wrote:
>>>>>
>>>>> Hi!
>>>>>
>>>>> You have to exclude AppFuse Data Common Package in your pom.xml.
Then
>>>>> add in to your poroject User and Role pojos at appfuse namespace (as
>>>>> described in http://www.appfuse.org/display/APF/AppFuse+Core+Classes
)
>>>>>
>>>>> Try to do  mvn clean compile hibernate3:hbm2ddl and check database
for
>>>>> user table structure. If it does not contains fields defined in User
>>>>> pojo of your project then AppFuse Data Common Package is not
excluded.
>>>>>
>>>>> What is your version of appfuse, M5?
>>>>>
>>>>> Regards,
>>>>> ros
>>>>>
>>>>>
>>>>>
>>>>> Fan wrote:
>>>>>>
>>>>>> Hey Ros:
>>>>>>
>>>>>> I did add the related Object to both hibernate.cfg.xml and
>>>>>> applicationContext.xml. But it just does not work
>>>>>>
>>>>>> Or do you mind to show me your hibernate.cfg.xml and
>>>>>> applicationContext.xml ? I afraid I might do it wrongly
>>>>>>
>>>>>> Is that necessary to exclude the AppFuse Data Common Package ?
>>>>>>
>>>>>>
>>>>>> ros wrote:
>>>>>>>
>>>>>>> Hi!
>>>>>>>
>>>>>>> I resolve that by add my Contact object to both hibernate.cfg.xml
>>>>>>> and applicationContext.xml.
>>>>>>>
>>>>>>> Reffer to http://www.appfuse.org/display/APF/AppFuse+Core+Classes
>>>>>>>
>>>>>>> Hope this helps.
>>>>>>>
>>>>>>> ros
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Fan wrote:
>>>>>>>>
>>>>>>>> Ros,
>>>>>>>>
>>>>>>>> What's the fix ? I am facing the same error as you did
>>>>>>>>
>>>>>>>>
>>>>>>>> ros wrote:
>>>>>>>>>
>>>>>>>>> Right. Thanks Matt!
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

--
View this message in context:
http://www.nabble.com/override-User-code-class-and-link-OneToMany-to-any-other-class-tf3217084s2369.html#a11448902
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--
/fadhli

Reply via email to