java - JPA throws no EntityExistsException but generates a duplicate row (auto generated PK) -


I'm new to JPA and I'm using a little bit in the hope of knowing the unit life cycle model. I expect EntityExists to be an exception in the code given below. However, instead of throwing the expected exception, it just creates a new line in the database table with a new primary key (increased by one).

Can anyone explain this? I suspect that there may be something with my auto-keying primary key in the respective table with @Generated Value Annotations on my ID field.

Below you can find 3 snippets: I try to run the code, my unit definition and my table design.

I would like to thank you in advance for shining your light on your misconceptions.

Merciful

ps: I use excelping as APLL and a derby database

script:

  Teacher Teacher = New Teacher ("John", "Do", "English"); // Continued John Doi EntityManager em = Perseverence.createEntityManagerFactory ("jpatest"). CreateEntityManager (); em.getTransaction () start () .; Em.persist (teacher); Committed to em.getTransaction (). Em.close (); // Try to do John Doe honestly for the second time. Honesty. Credit Entity Manager ("Jepettes"). CreateEntityManager (); em.getTransaction () start () .; Em.persist (teacher); Committed to em.getTransaction (). // I hope to throw here em.close (); Table Design: Make the table teachers (id INT as always the identity, first name VARCHAR (20), last name VARCHAR (40), primary key (ID));    

entity definition:

  package test package; Import javax.preistence.Entity; Import javax.preistence.GeneratedValue; Import javax.preistence.GenerationType; Import javax.preistence.Id; Import javax.preistence.Table; Import javax.preistence.Transient; @ Entity @Table (name = "teacher", schema = "USERNAME") Public Class Teacher {@Id @GeneratedValue (strategy = GenerationType.IDENTITY) Private Ent ID; Private string first name; Personal string last name; @ Transgen Private String Course; / ** * Zero logic maker / teacher for JAPA () {} public teacher (string first name, string last name, string course) {this.firstName = firstName; This.lastName = lastName; This.course = course; } Public int getId () {return ID; } Public string getFirstName () {return firstName; } Public string name () {last last name; } Public string getCourse () {return course; } Public Zero SetFirst-name (string first name) {this.firstName = firstName; } Public Zero setLlastName (String Last Name) {this.lastName = lastName; }}    

EntityExistsException will be thrown when you db

But here you have created an object with some objects and then automatically becomes unique for each persistence with the primary key that continues 2 times.

Because

  @ @ @ Generated Values ​​(strategy = generation type.INNNTTI)   

Every time you join the unit manager Continuous method calls and different institutions are related to Henk Eboth organizations. Because DB cares for the primary key to distinguish each keyboard.

So good you do to get such an exception

Get the unit from the first DB and change it by calling it on

  EntityManager em = Perseverence.createEntityManagerFactory ("jpatest"). CreateEntityManager (); Em.getTransaction () start () .; Teacher Teacher = em.find (Teacher Class, Primary Key) em.persist (Teacher); Committed to em.getTransaction (). Em.close ();   

Or remove the primary key automation and manual manually specify both time. Find me for any other question

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -