-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @OneToMany strange issue
PostPosted: Tue Apr 20, 2010 7:32 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I have the following:

Code:
public class Family extends BaseObject {
        private Parent wife = new Parent();
        private Parent husband = new Parent();
        ...

   @ManyToOne(optional=true)
   @JoinColumn(name="wife_id", nullable=true)
   @Cascade(value=org.hibernate.annotations.CascadeType.ALL)
   public Parent getWife() {
      return wife;
   }
   public void setWife(Parent wife) {
      this.wife = wife;
   }
   
   //husband's getter and setter analogically as wife's
}

public class Parent extends BaseObject {
        @OneToMany(cascade=CascadeType.ALL, mappedBy="??????????")
        public Family getFamily() {
                return family;
        }
        public void setFamily(Family family) {
                this.family = family;
        }
}


I would like to have an access to a Family object after loading a Parent object. I guess I should use @OneToMany mapping in my Parent object like above. If there was only one Parent in the family (wife for example) - there would be no problem:

Code:
public class Parent extends BaseObject {
        @OneToMany(cascade=CascadeType.ALL, mappedBy="wife")
        public Family getFamily() {
                return family;
        }
        public void setFamily(Family family) {
                this.family = family;
        }
}


But when there two Parent objects in Family object:wife and husband - I am not sure how to set mappedBy in Parent object.

The only way of resolve this problem which comes to my mind is to rearrange associations that it would be:

Code:
public class Parent extends BaseObject {
        boolean isWife;

        @ManyToOne
   @JoinColumn(name="family_id")
   @Cascade(value=org.hibernate.annotations.CascadeType.ALL)
        public Family getFamily() {
                return family;
        }
        public void setFamily(Family family) {
                this.family = family;
        }
}

[code]public class Family extends BaseObject {
        private Parent wife = new Parent();
        private Parent husband = new Parent();
        ...

   @OneToMany(cascade=CascadeType.ALL, mappedBy="family")
   public Parent getWife() {
      return wife;
   }
   public void setWife(Parent wife) {
      this.wife = wife;
   }
   
   //husband's getter and setter analogically as wife's
}


Is there another way of doing that (with ManyToOne association in the Family class)?

Regards,
Pawel


Top
 Profile  
 
 Post subject: Re: @OneToMany strange issue
PostPosted: Tue Apr 20, 2010 7:57 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Well the above solution does not work - I have tried it and have:

Quote:
org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: org.parafia.model.Family.wife


So not I do not know how to resolve that :/


Top
 Profile  
 
 Post subject: Re: @OneToMany strange issue
PostPosted: Tue Apr 20, 2010 9:01 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Ok, i can use:

Code:
public class Family extends BaseObject {
        private Parent wife = new Parent();
        private Parent husband = new Parent();
        ...

   @OneToOne(cascade=CascadeType.ALL, mappedBy="family")
   public Parent getWife() {
      return wife;
   }
   public void setWife(Parent wife) {
      this.wife = wife;
   }
   
   //husband's getter and setter analogically as wife's
}


It works, however I was going to solve this problem in another way.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.