-->
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.  [ 6 posts ] 
Author Message
 Post subject: Auto generated Primary key and Foreign Key
PostPosted: Tue Oct 23, 2007 7:15 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Hi All,
I have two table one is Event and second is EeventParam, primary key for event is auto generated, which is then used as foreign key in EventParam and there is one to many relationship between these two table.I want to persists objects in these two table in single session.save(Event e) call. but it is giving me ConstarintVailotion exception. After degugging it, i came to know that while peristing EventParam object, its is not able to insert FK which gets from Event table where it is auto generated.Can anybosy tell me the solution for this, how i can get the auto generated key in advance prior to save in DB.

Babu.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 10:27 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
It will be better if you post your mapping files.

A simple mapping could be like

<class name="Event" table="EVENT">
<set name="eventParams" cascade="all-delete-orphan">
<key>
<column name="EVENT_ID"/>
</key>
<one-to-many class="zzz.EventParam"/>
</set>
..
..
..
</class>

<class name="EventParam" table="EVENTPARAM">
..
..
..
</class>

See this for more info on collection mapping

http://www.hibernate.org/hib_docs/v3/re ... ollections


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 12:37 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Here, I am using hibernate annotation instead of .hbm.xml file,

following are my Event and EventParam entity with their mapping:-

@Entity
@Table(name = "EVENT")
public class Event {
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ACTIVITYID")
public List<ActivityParam> activityParam;
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
----
-----------
}
-------------------------------
EventParam.java

@Entity
@Table(name = "ACTIVITY_PARAM")

public class EventParam {
@ManyToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name =EVENTID",
referencedColumnName = "ID")
public Event event;
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@Column(name = "EVENTID")
private long eventId;
.....
.........

}

Now ,my problem is when i try to persist the EevntParam in DB i have all the attribut from Event except the ID which is foreign key in EventParam,for which my Event entity should be get persist first then the EventParam. Here i am little cofused about the sequence of insert to DB, can you please tell me in which sequence it works, i mean Parent table get inserted first or Child, also give me the solution for above case. Thanks in advance.

Babu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 1:49 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
I have never worked with annotations but I think it should be something like:



@Entity
@Table(name = "EVENT")
public class Event {
@OneToMany(cascade = CascadeType.ALL,CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "ACTIVITYID")
public List<ActivityParam> activityParam;
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
----
-----------
}
-------------------------------
EventParam.java

@Entity
@Table(name = "ACTIVITY_PARAM")

public class EventParam {
@PrimaryKeyJoinColumn(name =EVENTID",
referencedColumnName = "ID")
public Event event;
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@Column(name = "EVENTID")
private long eventId;
.....
.........

}

http://www.hibernate.org/hib_docs/annot ... ec-cascade


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 2:48 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
I think, you have not understood my problem, i want the auto generated primary key from parent table which is then used in Child table as foriegn key before calling any session.save().

Bb


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 3:13 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
May be I am missing something. I was just suggesting that hibernate do have a provision where saving a parent object would save all unsaved child objects with proper referential integrity and you do not need to worry about primary / foreign keys.

_________________
Please rate this post if you find it helpful


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