-->
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: One-to-many, join-tables, foreign-key best practices
PostPosted: Thu Jan 19, 2006 9:11 pm 
Newbie

Joined: Thu Jan 12, 2006 6:02 pm
Posts: 9
Hibernate version:
3.1 (3.1 beta6 annotations)

I have a typical one-to-many relationship between parent and child. In our case, however, the parent has a lot of these one-to-many and I don't want to put the mapping for all of them in the parent class (don't want to have to load the entire database). Plus, in the UI, most of the child objects are edited/manipulated "on their own". Additionally, the child object could be re-used across many parent objects. For example, the parent is a Customer and the child is a Note. There could not only be Customer notes, but potentially Location notes, Employee notes, ....

I'm wondering what the best practice is for mapping these. I know the documenation indicates a @JoinTable, but I'm not sure how to do this if I don't put any mapping in the parent.

Parent
ID (pk)

Child
ID (pk)

Join
PARENT_FK
CHILD_FK


Alternatively, I could put the foreign key in the child (I know, not recommended). However, I don't really want to load the parent when loading the child. Is there a way (with annotations) to enforce referential integrity without having to load the parent?

Child
ID (pk)
PARENT_FK (fk)


My UI services would call typical crud on the parent and child separately, and since I don't necessary want to load all of the children with the parent, I would also call services such as:

Parent parent = getParent(parentId);
Child child = getChild(childId);
List children = getChildrenByParentId(parentId);


Hopefully this makes some sense. How do y'all solve this?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 9:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Either
1) map it correctly, and make use of lazy initialization, or
2) map the foreign key fields (of the least-frequently used child objects?) as property fields instead of entity references (one-to-many or what have you) and use factory methods to load the children only when you need them.

Option 2 sounds like it might be more appropriate for you, though obviously it means that you'll have to write some code that hibernate already provides for you. But at least it guarantees that you won't accidentally load the whole database..


Top
 Profile  
 
 Post subject: One-to-many, join-tables, foreign-key best practices
PostPosted: Fri Jan 20, 2006 9:13 am 
Newbie

Joined: Thu Jan 12, 2006 6:02 pm
Posts: 9
If I go with option two (property), how can I annotate my pojo so that referential integrity is ensured. That is, what annotation can I add so that the appropriate FK constraint is created when I run hbmddl? I'm not sure how to do this without having a reference to the entire parent object.

Thanks,

Jim


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 4:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can't, in your mapping file. You'll have to do that directly at the DB.


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.