• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicated rows on the database

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can it be related to the implementation of equals and hashcode?

https://www.hibernate.org/109.html
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic