-->
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.  [ 3 posts ] 
Author Message
 Post subject: Get the id of an entity without loading it?
PostPosted: Tue Aug 09, 2005 4:41 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Say I have a join table that joins table1 and table2. This join table is going to define equals and compareTo based on table1 and table2, so I would probably be tempted to write this:

Code:
compareTo (JoinTable rhs) {
   return new CompareToBuilder()
      .append(getTable1, rhs.getTable1())
      .append(getTable2, rhs.getTable2())
      .toComparison()
}


Say I query for a list of these objects, then throw them all in a TreeSet. Will I then get two selects for every object because TreeSet is called getTable1() and getTable2() all over the place? If so, is there a way to define equality on the IDs, rather then the full objects?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 7:58 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Yes, just call the identifier getter.

.append(getTable1().getId(), rhs.getTable1().getId())

Invoking the identifier getter does not cause initialization of uninitialized proxies, since the identifier value of a many-to-one is loaded along with the parent entity, even if the object model tends to obscure that.

In truth, you'd probably do something like this:

private static Long getId(Identifiable obj)
{
return obj == null ? null : obj.getId();
}
...
.append(getId(getTable1()), getId(rhs.getTable1())

So as to avoid NPEs when sorting transient objects.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 3:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
using id's for equals/hashcode has issues - see wiki about equals and hashcode.

_________________
Max
Don't forget to rate


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