Forums Register Login

Duplicated rows on the database

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Can it be related to the implementation of equals and hashcode?

https://www.hibernate.org/109.html
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 


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.
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1854 times.
Similar Threads
Adding associations to a join entity..
unwanted cascade delete behaviour
hashCode() & equals() method test
Problem at insert with relationship JPA 2.0
inheritance and schema creation using JPA annotations
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:47:11.