-->
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.  [ 11 posts ] 
Author Message
 Post subject: Primary key Foreign key relationship in hibernate
PostPosted: Wed Jan 03, 2007 8:42 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
HI ,

I have two tables EventObject and EventDetails.I want primary key of EventObject to be foreign key in EventDetails i.e mapping from EventObject to EventDetail shoud be one-to-many and the mappping is unidirectional it need not be bidirectional.

My mapping files are as follows

1)EventObject
<hibernate-mapping>

<class name="EventObject" table="EVENTOBJECT">
<id name="eventId" column="id">
<generator class="native"/>
</id>

<property name="USERNAME" column="USERNAME"/>
<property name="SESSIONID" column="SESSIONID"/>
<property name="OPERATIONTYPE" column="OPERATIONTYPE"/>
<property name="TIMESTAMP" column="TIMESTAMP"/>
<property name="VENDOR" column="VENDOR"/>
<property name="IPADDRESS" column="IPADDRESS"/>
<property name="FAILURECOUNT" column="FAILURECOUNT"/>
<set name="evenDetails" cascade="save-update">
<key column="EVENTID"/>
<one-to-many class="EventDetails" />
</set>

</class>

</hibernate-mapping>

2)EventDetails

<hibernate-mapping>

<class name="EventDetails" table="EVENTDETAILS">
<id name="id" column="EventDetails_ID" type="long">
<generator class="native"/>
</id>

<property name="FIELDNAME" column="FIELDNAME"/>
<property name="OLDVALUE" column="OLDVALUE"/>
<property name="NEWVALUE" column="NEWVALUE"/>
</class>

</hibernate-mapping>

The problem here is that i need to create a primary key for second mapping.Is there a way by which i can have primary key of EventObject(which is foreign key here) + FIELDNAME of EventDetials table as primary key in EventDetails table

Plz let me know if this is possible and also if the above mapping is correct

Thanks in advance
Akshay


Top
 Profile  
 
 Post subject: Try key-many-to-one
PostPosted: Wed Jan 03, 2007 9:35 am 
Beginner
Beginner

Joined: Wed Aug 31, 2005 3:54 am
Posts: 45
<composite-id
name="propertyName"
class="ClassName"
<key-property name="propertyName" type="typename" column="column_name"/>
<key-many-to-one name="propertyName class="ClassName" column="column_name"/>
......
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 9:56 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
Hi can u elaborate a little


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 10:00 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
I think this is possible only when the association is bi directional....... my association is unidirectional.......


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 10:04 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
HI Aks,
I already posted a reply and somehow disappered.

By making Event PK as EventDetails PK.You will end up having a one to one relations.

EventObject
<one-to-one name="entity" class="EventDetails" constrained="true"/>

EventDetails
<one-to-one name="entity" class="EventObject" constrained="true"/>

Is it what you wanted ?
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 10:10 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
EventObject
<one-to-one name="entity" class="EventDetails" >

EventDetails
<one-to-one name="entity" class="EventObject" constrained="true"/>


Sorry you should have constrained on the EventDetails not on EventObject.
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 10:34 am 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
Hi no my requirement is one to many from EventObject to EventDetails


Top
 Profile  
 
 Post subject: key-many-to-one is not bidirectional
PostPosted: Wed Jan 03, 2007 10:48 am 
Beginner
Beginner

Joined: Wed Aug 31, 2005 3:54 am
Posts: 45
<key-many-to-one name="propertyName class="ClassName" column="column_name"/>

I don't think that key-many-to-one is bidirectional ...
Have you try ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 11:53 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
You at the topics 7.2.3 and 7.3.1. one to many


http://www.hibernate.org/hib_docs/v3/re ... ations.htm

This will solve your problems.
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject: Primary key -foreign key relationship
PostPosted: Wed Jan 03, 2007 1:45 pm 
Beginner
Beginner

Joined: Sun Nov 19, 2006 6:18 am
Posts: 28
I went thru the link but it says how to make primary key of one table as foreign key of another but it never says how to make this foreign key + FIELDNAME in my case as a primary key combination


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 2:02 pm 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
Well I dont get it correctly then.Can you explain with a example of what you want ?
Like show some data for
EventObject

pk

1
2



Event Details
pk eventId
1 2
2 1
3 1
4 2

The above is my example

What do u mean by "how to make this foreign key + FIELDNAME in my case as a primary key combination "

In that case you can create a composite Key

Create a Java Class EventID and let it has both unique identifier and also EventObject PK

In that case

Event Details
PK ---- This 2 fields combined to make one PK
-------------------
seq eventId
1 2
2 1
3 1
4 2

so in your mapping just replace the EventDetails PK definition.So I believe you get the combination of SEQ + EVENT OBJECT FK ..
also alter the code to show EventID is also the Foriegn key in your DB Defn.
Still your mapping for Event Object wont change.
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


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