Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Duplicated rows on the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Duplicated rows on the database" Watch "Duplicated rows on the database" New topic
Author

Duplicated rows on the database

Luiz Abrahao
Greenhorn

Joined: Jun 18, 2008
Posts: 10
Hi!

I'm confused and I think I'm missing some concept.

The sort version: The entity is persisted twice if I call childEntityDAO.makePersistent(e).

The long version: I have 2 entities, Company and Division.





The DAO layers is implemented as described in the section 16.4.2 of Java Persistence with Hibernate. If I try to save a Division like the follow listing it works fine:



But if I call the divisionDAO.makePersistent(d) I got 2 identical (apart form the ID column) rows on my database. I need to call the makePersistent method because I need to get the ID of the persisted entity to use on my tests.

I found out that there is a bug (http://opensource.atlassian.com/projects/hibernate/browse/HHH-3810) that affects the version of the hibernate I was using.

I updated to the 3.3.2 but it didn't stop the entity to be persisted twice.

As far as I understood from the book if I call the d = divisionDAO.makePersistent(d) should be ignored. There must be something wrong with my mapping then.

Any ideas appreciated,
Luiz Filipe
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Can it be related to the implementation of equals and hashcode?

https://www.hibernate.org/109.html
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yeah, that's pretty whacky logic for equality you have there - if a Department has a Folder, use that to test equality, otherwise use the primary key. Why do you not just use the ID?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Luiz Abrahao
Greenhorn

Joined: Jun 18, 2008
Posts: 10
Thank you for your reply!

These entities were ported from hibernate to JPA, I only added the annotations though, both hashCode and equals methods were generated by eclipse. I have applications using these entities for quite long time, with no problems.

Edwin, I read the article you send, unfortunately I can't spot anything wrong with the entities' hasCode and equals.

I found something interesting, and I posted it here: https://forum.hibernate.org/viewtopic.php?f=1&t=1001405. I have the feeling that this problem is something related with the my tests structure.

Paul Sturrock wrote:Yeah, that's pretty whacky logic for equality you have there - if a Department has a Folder, use that to test equality, otherwise use the primary key. Why do you not just use the ID?


Sorry Paul, I don't know if I understood well, because either folder and id are already being used to test equality.

Again, thank you for your time
Luiz Filipe
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Sorry Paul, I don't know if I understood well, because either folder and id are already being used to test equality.

So, do you really have a composite key here? Remember a primary key cannot be null, and equality in a database application is done by primary key. If you have a nullable field in there there is something wrong with your data modelling.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Duplicated rows on the database