-->
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.  [ 4 posts ] 
Author Message
 Post subject: Inheritance with one-to-many relation
PostPosted: Tue Dec 30, 2008 8:52 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
Hi,

I have a Parent class and two children - ChildA and ChildB - classes.
I need to map these to three tables - PARENT, CHILDA and CHILDB.
PARENT to CHILDA is an optional one-to-one relation.
PARENT to CHILDB is an optional one-to-many relation.
I am able to map ChildA with a joined-subclass, and it works fine.

How do I map the ChildB? The primary key of the CHILDB contains the primary key of the PARENT, and another column(say SEQ_NUM).

Thanks, LinHib.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 9:47 pm 
Newbie

Joined: Fri Nov 21, 2008 3:35 pm
Posts: 12
XML Mapping:
http://www.hibernate.org/hib_docs/v3/re ... tance.html

And if you're using Annotations/JPA:
http://www.hibernate.org/hib_docs/annot ... tml#d0e829

Always be sure to check the docs ;)
If this helps, would you do me a favor and mark the post helpful below? Thanks,
- TK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 3:30 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
I am using the "table per subclass" strategy for the PARENT-to-CHILDA relation, and it works.

But I am not able to use the same for PARENT-to-CHILDB relation since the primary key of CHILDB is not the same as the primary key of the PARENT. The primary key of CHILDB is (PARENT_ID, SEQ_NUM), where as the primary key of the PARENT is PARENT_ID alone.

class Parent {
Long parentId;
}

class ChildB extends Parent {
Collection<ChildBElement>;
}

class ChildBElement {
Long seqNum;
...
}

I defined a joined-subclass for CHILDB with key as PARENT_ID. Added a bag for the collection, with key as the SEQ_NUM. Now hibernate tries to insert a record into CHILDB with only PARENT_ID, and fails since SEQ_NUM is not nullable:
Generated SQL: insert into CHILDB (PARENT_ID) values (?)

It doesn't insert SEQ_NUM and other values in the ChildBElement. What am I doing wrong?

Thanks, LinHib.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 6:45 pm 
Newbie

Joined: Tue Dec 30, 2008 6:08 am
Posts: 6
Location: India
LinHib wrote:
I am using the "table per subclass" strategy for the PARENT-to-CHILDA relation, and it works.

But I am not able to use the same for PARENT-to-CHILDB relation since the primary key of CHILDB is not the same as the primary key of the PARENT. The primary key of CHILDB is (PARENT_ID, SEQ_NUM), where as the primary key of the PARENT is PARENT_ID alone.

class Parent {
Long parentId;
}

class ChildB extends Parent {
Collection<ChildBElement>;
}

class ChildBElement {
Long seqNum;
...
}

I defined a joined-subclass for CHILDB with key as PARENT_ID. Added a bag for the collection, with key as the SEQ_NUM. Now hibernate tries to insert a record into CHILDB with only PARENT_ID, and fails since SEQ_NUM is not nullable:
Generated SQL: insert into CHILDB (PARENT_ID) values (?)

It doesn't insert SEQ_NUM and other values in the ChildBElement. What am I doing wrong?

Thanks, LinHib.


Cheak that you are cascading from bag.can you tell exactly what is the exception you are getting


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