-->
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: General practice - foreign keys and entities versus keys
PostPosted: Sat Feb 18, 2006 5:44 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
This is more of a general practices question than a technical question. I have a fairly normalized database, and one of my tables has about 8 foreign keys (most of them nullable). Would it be better to store these foreign keys in the entity as java.lang.Long, store them as target entities, or both? If both, how do I keep the two synchronized when I update one and not the other?

I'm finding that in a lot of instances, all I need to access is the foreign key, which means I have to call entity.getTarget().getId(), for example, rather than just entity.getTargetId(). Is this the preferred way of doing things?

I tried the "both" method, but when all I need to work with is the key, I find the target entity (annotated by insertable=false,updatable=false since the target column appears twice in the entity class) doesn't get updated, and I'm not sure if this is my responsibility or not.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 18, 2006 7:18 pm 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I have some situation.

I have a table with many foreign key and when I save a new instance of coresponding java mapping I must select each instance for many-to-one relation.
I've already values for every foreign key but I must find the instance of coresponding class. This is annoing, cose I do a lot of db roundtrip for every foreign key even if I already have the value of foreign key.

If I try to map another property(ordinary) for foreign key, that map to the same column as many-to-one mapping, hibernate force this new property to be update="false" insert="false" :(


I've already post a topic about this few days ago but nobody answered.


Top
 Profile  
 
 Post subject: Integration with struts and EL
PostPosted: Sat Feb 18, 2006 7:46 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
I'm finding the entity mapping to be a benefit in JSP, however, as I can get all the properties I need and print them to the page without having to do any manual loading:

<table>
<tr>
<td>First</td>
<td>${entity.first.id}</td>
<td>${entity.first.description}</td>
</tr>
<tr>
<td>Second</td>
<td>${entity.second.id}</td>
<td>${entity.second.description}</td>
</tr>
</table>

Which is great when I need to display details about associated entities, but not so great when all I'm working with is keys.

I've considered including both in my entity class, but instead of providing the entity mapping, I'd provide the key mapping (as java.lang.Long), and make a read-only accessor getTarget() that would load the target entity on demand. After all, if I have the target entity already loaded in a way that I could do a setTarget( target ), I can simply do a setTargetId( target.getId() ), and still do the setTargetId( targetId ) when I don't have the target entity loaded.

But loading the entity on demand in the accessor would cause even more round-trips to the database when I need to display multiple properties of the target entity. Hence my original post :).


Top
 Profile  
 
 Post subject: Hibernate FAQ
PostPosted: Mon Feb 20, 2006 11:07 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Hey tonyXXX, just wanted to let you know, I found this in the FAQ (Tips and Tricks):

---------------------
How can I create an association to an entity without fetching that entity from the database (if I know the identifier)?

If the entity is proxyable (lazy="true"), simply use load(). The following code does not result in any SELECT statement:

Code:
Item itemProxy = (Item) session.load(Item.class, itemId);
Bid bid = new Bid(user, amount, itemProxy);
session.save(bid);


How can I retrieve the identifier of an associated object, without fetching the association?

Just do it! The following code does not result in any SELECT statement, even if the item association is lazy.

Code:
Long itemId = bid.getItem().getId();

---------------------

So by the sounds of things, just going ahead and encapsulating the entities is the way to go. Just be careful of entity.getTarget().getId() when the foreign key can be null.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 4:11 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
Thx a lot silvaran. It's cool man. I will try it and use it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 4:58 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 12:37 pm
Posts: 47
Location: Romania, Galati
I tried it and worked well. No more selects for known foreign keys. It's cool with proxies.

Thx a lot silvaran.


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.