-->
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.  [ 13 posts ] 
Author Message
 Post subject: How do I tell Hibernate to use equals() for table entries?
PostPosted: Sat Oct 16, 2004 6:34 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
Hi,

I've got two tables:

1) Image
2) ImageSpecification

Image.getSpecification() returns an object of type ImageSpecification

The problem I'm experiencing is that I see multiple ImageSpecification instances in the database with the exact same values, except for id. Since "id" is the primary key, the table allows this insertation.

My question is, how do I tell Hibernate: if you see another ImageSpecification in the table such that equals() returns true, use it instead of creating a new instance?

Thank you,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 6:53 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
PS: I am aware I can explicitly look for duplicates in the table and use one if it exists. What I am asking for is a way to tell Hibernate to do this transparently.

Does this exist?

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 6:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, no. Which one of your duplicates should Hibernate chose? We use the primary keys to distinquish tuples (as does your database) and if your primary keys are not the right way to identify a row, you should start thinking about your database content, esp. if its in a good and proper state or not.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 6:58 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Here is a hint: As a test of the integrity and correctness of your data, assume that "ImageSpecification" contains the size of the image in bytes. Now run an aggregate function, to find out how much bytes all of your images have. Getting the correct result?

P.S. "distinct" is not the right way to do it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:13 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
Uhm, no. Which one of your duplicates should Hibernate chose? We use the primary keys to distinquish tuples (as does your database) and if your primary keys are not the right way to identify a row, you should start thinking about your database content, esp. if its in a good and proper state or not.


Christian,

I could use custom IDs and map id=hashCode() and that would likely fix the problem. The Hibernate documentation discourages the use of custom IDs, but would it not be appropriate in this case? As long as I ensure that hashCode() is unique (i.e. no collisions for ImageSpecifications with different values) it should be an appropriate solution, right?

Another problem is that updateOrSave() will no longer work, which is a bummer.

What do you think?

Thanks,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Really, stop thinking about Java and Hibernate, but think about your database! What use is duplicate information? Would you say it adds value or is it even possible that it is the source for false information?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:21 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
Really, stop thinking about Java and Hibernate, but think about your database! What use is duplicate information? Would you say it adds value or is it even possible that it is the source for false information?


I would say that duplicate information in the database is just a waste of space. I could allow duplication entries (no harm would be caused, it is not the source for false information) but I'd rather have at most one entry per possible ImageSpecification in the table.

In your example of ImageSpecification = image size, I would say I could scan all my images, get a list of all unique sizes and add them to the table. If a new image is added it uses a preexisting ImageSpecification if its size is already listed, otherwise it creates and adds a new one.

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:26 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, if its only a waste of space, I'll rest my case. We have different opinions about relational databases and data management. ;)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:36 pm 
Senior
Senior

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 151
christian wrote:
Well, if its only a waste of space, I'll rest my case. We have different opinions about relational databases and data management. ;)


Christian,

I don't understand. I know I've mentioned a specific use-case but I'm actually asking a theoretical question about Hibernate. Opinions about relational databases and data management should not matter.

On the topic of this specific case, can you please explain to me what your preferred course of action is? You mention a difference of opinion but I don't know what your opinion is :) Please enlighten me.

Thank you,
Gili


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:37 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Clean up your broken database.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 7:46 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
I know I've mentioned a specific use-case but I'm actually asking a theoretical question about Hibernate. Opinions about relational databases and data management should not matter.


If we'd have a quote of the week, this would be it :) Questions about Hibernat can't possibly have something to do with relational databases. Wow...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Re: How do I tell Hibernate to use equals() for table entrie
PostPosted: Sun Oct 17, 2004 2:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
cowwoc wrote:
My question is, how do I tell Hibernate: if you see another ImageSpecification in the table such that equals() returns true, use it instead of creating a new instance?


You can

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 17, 2004 2:21 am 
Newbie

Joined: Sat Oct 16, 2004 4:51 pm
Posts: 9
Christian,

Do you think the question I posted http://forum.hibernate.org/viewtopic.php?t=935398 is little bit related to what is described here
Quote:
The problem I'm experiencing is that I see multiple ImageSpecification instances in the database with the exact same values, except for id. Since "id" is the primary key, the table allows this insertation.

I also trying to avoid adding multiple columns in tables with exactly same data.

It will be great help if you can shed some more light on http://forum.hibernate.org/viewtopic.php?t=935398

Thanks in advance


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