Persisting a new but identical entity with JPA reporting duplicate entry?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

I don't think there is any standard way to specify operational ordering in JPA, so there is no guarantee which order the statements will be executed. Ideally, the JPA implementation would be clever enough to detect this situation and perform the delete before the insert, but that is an area they frequently fail in. Alternatively, if your database supported deferred constraint checking (e.g. , Oracle does but MySQL doesn't), the database would handle this by waiting until commit time to give the unique constraint violation exception.

I don't think there is any standard way to specify operational ordering in JPA, so there is no guarantee which order the statements will be executed. Ideally, the JPA implementation would be clever enough to detect this situation and perform the delete before the insert, but that is an area they frequently fail in. Alternatively, if your database supported deferred constraint checking (e.g. , Oracle does but MySQL doesn't), the database would handle this by waiting until commit time to give the unique constraint violation exception.

So one solution is to just perform an extra commit after your call to remove(entity1). Another possibility would be, before you create entity2, to first check to see if it exists in the database, and if so, just use that one. Both these options can be somewhat cumbersome and not be suitable for all workflows.

You might want to dig into the documentation for your current JPA implementation to see if they offer any extensions that might help.

This is definitely pointing me in the right direction. I'm now seeing references to JPA (I'm using EclipseLink, specifically) doing inserts before deletes by default. The application allows the user to save or cancel at any time, so I can't do a commit after the remove, but I'm looking into either changing that default or building in the reuse functionality.

– Jon Dec 19 '09 at 19:36 I suspect you wanted to say: an explicit flush() after the remove(entity1). No need to commit, and it is a bad idea (better put the whole method in one TRA) – pihentagy Jul 26 '10 at 16:24.

I had the same problem involving a UC and the insertions/deletions occurring in the wrong order. I had to create a composite key for my entity that matched the columns of the UC. After that, the deletions and insertions occurred in the correct order.

See this post, and the comment about adding an @EmbeddedId: https://forum.hibernate. Org/viewtopic. Php?

P=2382504.

I think I get the general idea, but I'm a little lost on the specifics. Could you give a simple example? Say, what if I have a simple table of people for contact info.

I have a unique constraint in the database for the person's name (id int, name varchar), and other tables link to that id. I'm editing my address book and go, "Oh, I'm not friends with Eddie anymore," so I delete that entry, cascading to any other associated data. But then after updating some other people (and not saving), I enter "Eddie" as an entry for someone else entirely.

Under the JPA I designed, that throws an error. – Jon Jan 29 '10 at 17:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions