-->
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: PK/FK mapping issue
PostPosted: Wed Jul 16, 2008 12:40 am 
Newbie

Joined: Wed Jul 16, 2008 12:27 am
Posts: 5
Hi,

I have 2 tables with Parent to Child relationship of one to many.
Parent (A):
col_1 (Generated PK from a sequence)
col_2
...

Child (B):
col_1 (PK and FK from A)
col_3 (PK)
col_4
...

I have used cascade="all" in parent
After making the object, when i do the saveOrUpdate, the child is getting null value for col_1.
I did read that it do first insert and then update on child, but it was also said that bidirectional would solve this problem. I tried that also, but still getting null value in child.

Is there a way to get Parent's primary key in Child?
Let me know if i need to give more information.

Thanks in advance.

_________________
-- Jyoti


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 2:08 am 
Newbie

Joined: Wed Jul 16, 2008 12:27 am
Posts: 5
Couple of other information

Hibernate version: 3.2
Mapping documents:hibernate.cfg.xml

Code between sessionFactory.openSession() and session.close():

A a = getObjectA();
B b = getObjectB();

b.setA(a);

Set<B> bSet = new HashSet<B>();
bSet.add(b);

a.setB(bSet);

session.saveOrUpdate(a);

Name and version of the database you are using: Oracle

Snippet from xml mapping files:
A.hbm.xml:

<set name="details" inverse="true" cascade="all">
<key column = "col_1"/>
<one-to-many class="B"/>
</set>

B.hbm.xml:

<composite-id>
<key-property name="col_1" column="col_1"/>
<key-property name="col_3" column="col_3"/>
</composite-id>

<many-to-one name="pkg"
column="col_1"
insert="false" update="false"
not-null="false"/>

_________________
-- Jyoti


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 3:18 am 
Newbie

Joined: Sat Dec 25, 2004 11:18 pm
Posts: 11
Hi Jyoti

I would try following two things:

1. Since you are trying to save collection while saving 'a', remove inverse = true attribute from mapping for collection in a.hbm.

2. Add not-null = true in key property for collection as :
<key column="col1" not-null="true"/>

Regards

Manan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:54 am 
Newbie

Joined: Wed Jul 16, 2008 12:27 am
Posts: 5
Hi Manan,

Thanks for quick response.
I tried these 2 things, but it's still not working. Removing inverse="true" didn't have any effect. But when i added not-null="true" in key, its giving error, even before i started the session. Probably at mapping time.

This is the stack trace.
Quote:
Repeated column in mapping for entity: B column: col_1 (should be mapped with insert="false" update="false") { org.hibernate.MappingException: Repeated column in mapping for entity: B column: col_1 (should be mapped with insert="false" update="false") | at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:652) | at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:674) | at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696) | at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:450) | at org.hibernate.mapping.RootClass.validate(RootClass.java:192) | at org.hibernate.cfg.Configuration.validate(Configuration.java:1102) | at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
<snip>


In B.hbm.xml i am giving
insert="false" update="false"

What am i doing wrong?

_________________
-- Jyoti


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 2:07 pm 
Newbie

Joined: Sat Dec 25, 2004 11:18 pm
Posts: 11
Hi Jyoti

It looks like that you have col1 mapped twice in your HBM file for entity B.

It should only be mapped once. Look for col1 in HBM, make sure it is mapped only once.

Regards

Manan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 12:38 am 
Newbie

Joined: Wed Jul 16, 2008 12:27 am
Posts: 5
Hi Manan,

In the hbm file, the col_1 is mentioned in composite key and at the many-to-one.

But i am giving insert="false" update="false" with many-to-one.
This is working in other hbm files. The error mentioned to use the same thing.

Any idea?

Regards,
Jyoti


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.