-->
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.  [ 5 posts ] 
Author Message
 Post subject: @OneToMany unidirectional relation problems
PostPosted: Fri Nov 07, 2008 10:39 am 
Beginner
Beginner

Joined: Wed Apr 23, 2008 2:00 pm
Posts: 20
Hello,
I have defined two classes like this :

@Entity
@Table (name="PROJECT_ACTIVITIES")
@SequenceGenerator(name = "SEQ_PACT_ID", sequenceName = "SEQ_PACT_ID", allocationSize = 1)
public class Activity implements IsSerializable{
@Id
@Column(name="PACT_ID", nullable=false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PACT_ID")
private Integer id = null;
@Column(name="PACT_DESC")
private String desc = null;
@Column (name ="STATE")
private Integer state = null;
@Column (name ="PROJ_ID")
private Integer idProject = null;
@Column (name ="PACT_DUE_DATE")
private Date dueDate = null;

@OneToMany (mappedBy = "pactId")
@JoinColumn (name = "PACT_ID", referencedColumnName="PACT_ID", nullable=false)
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
private List<AssignedActivity> assignedTo = new ArrayList<AssignedActivity>();

... }

@Entity
@Table(name = "ASSIGNED_ACTIVITIES")
@IdClass(AssignedActivityPk.class)
public class AssignedActivity implements IsSerializable {
@Id
@Column (name = "CONS_ID", nullable=false)
private Integer consId;
@Id
@Column (name = "PACT_ID", nullable=false)
private Integer pactId;
@Column (name = "TYPE")
private Integer type;
@Column (name = "ASG_EST_TIME")
private Integer asgEstTime;
...}

As the ud from the parent class (Activity) is generated by a sequence from de database, when I'm trying to insert a new Activity, the record for parent object is correct (the id is generated) but not for the child. So i've the child inserted but width the pact_id = null.

How can i tell to hibernate to copy the generated pact_id from the parent??

thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 3:17 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In the AssignedActivity you need to map the Activity object, not the Integer ID value. Map it something like this with @ManyToOne:

Code:
@ManyToOne
@JoinColumn(name="PACT_ID")
private Activity activity;


See http://www.hibernate.org/hib_docs/annot ... ollections for several examples


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 8:26 am 
Beginner
Beginner

Joined: Wed Apr 23, 2008 2:00 pm
Posts: 20
Thank you! That worked.
I thougt i was a way to implement this just unidirectional relation...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 2:51 pm 
Beginner
Beginner

Joined: Wed Apr 23, 2008 2:00 pm
Posts: 20
Now it's not working and i don't understand why..
The annotations in my classes are :

Activity{

@OneToMany (mappedBy = "pactId", fetch = FetchType.LAZY)
@JoinColumn (name = "PACT_ID", referencedColumnName="PACT_ID", nullable=false)
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
private Set<AssignedActivity> assignedTo = new HashSet<AssignedActivity>();
...
}

AssignedActivity{
@Id
@Column (name = "CONS_ID", nullable=false)
private Integer consId;
@Id
@Column (name = "PACT_ID", nullable=false)
private Integer pactId;
@Column (name = "TYPE")
private Integer type;
@Column (name = "ASG_EST_TIME")
private Integer asgEstTime;

@ManyToOne
@JoinColumn (name = "PACT_ID", nullable=false, insertable = false, updatable = false)
private Activity activity;
...
}

Now it continues inserting the registar width pact = NULL. Do you know which is the problem?
Thanks!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 9:45 am 
Beginner
Beginner

Joined: Wed Apr 23, 2008 2:00 pm
Posts: 20
Finally i've foud a solution.. maybe will help someone.
The think was that i've a OneToMany-ManyToOne relation (bidirectional), an activity has multiple AssignedActivities and a AssignedActivity is relationated width an Activity.

Here are my Classes :
Code:
@Entity
@Table (name="PROJECT_ACTIVITIES")
@SequenceGenerator(name = "SEQ_PACT_ID", sequenceName = "SEQ_PACT_ID", allocationSize = 1)
public class Activity implements IsSerializable{
   
   @Id
   @Column(name="PACT_ID", nullable=false)
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PACT_ID")
   private Integer id = null;
   @Column(name="PACT_DESC")
   private String desc = null;
   @Column (name ="STATE")
   private Integer state = null;
   
   @OneToMany (mappedBy="pk.activity", fetch = FetchType.LAZY)
   @Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN, CascadeType.SAVE_UPDATE})
   private Set<AssignedActivity> assignedTo = new HashSet<AssignedActivity>();
...}


@Entity
@Table(name = "ASSIGNED_ACTIVITIES")
public class AssignedActivity implements IsSerializable {
   @EmbeddedId
   private AssignedActivityPk pk = new AssignedActivityPk();
...}


@Embeddable
public class AssignedActivityPk implements Serializable {
   @Column (name = "CONS_ID", nullable=false)
   private Integer consId;
   
   @ManyToOne
   @JoinColumn (name = "PACT_ID", nullable=false)
   @ForeignKey(name="PACT_ID")
   private Activity activity = new Activity();
...}

and then to save and update my records i'm doing :

Code:
if ( a.getId() != null) {
   Activity act = (Activity) session.load(Activity.class, a.getId()) ;
   act.setDesc(a.getDesc());
   act.setDueDate(a.getDueDate());
   act.setPersonInCharge(a.getPersonInCharge());
   act.setState(a.getState());
   act.clearAssignedTo();
   session.saveOrUpdate(act);   
   session.getTransaction().commit();
   session.getTransaction().begin();
   Iterator it = a.getAllAssignedTo().iterator();
   while (it.hasNext()) {
      AssignedActivity aa = (AssignedActivity)it.next();
      aa.setActivity(act);
      act.addAssignedTo(aa);   
   }
   session.saveOrUpdate(act);   
} else {
   session.persist(a);   
}
session.getTransaction().commit();



To update the records i delete first the old records and then create the new ones. After long time, this is the solution i've found which is working... if somebody has a better solution please let me know.

Regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.