Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicated rows on the database

 
Luiz Abrahao
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can it be related to the implementation of equals and hashcode?

https://www.hibernate.org/109.html
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Luiz Abrahao
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic