I have a problem with Hibernate EntityManager.
Here is my DAO function:
public Boolean create(T element)
{
System.out.println("To persist: "+element);
System.out.println("PersistanceHolder: "+holder);
System.out.println("EntityManager: "+entityManager);
System.out.println("CRUDDao: "+this);
System.out.println("Thread: "+Thread.currentThread());
entityManager.getTransaction().begin();
try
{
entityManager.persist(element);
}
catch(EntityExistsException e)
{
entityManager.getTransaction().rollback();
return Boolean.FALSE;
}
entityManager.getTransaction().commit();
return Boolean.TRUE;
}
My Element is this:
@Entity
public class Event {
@Id
String name;
Maturity maturity;
Integer delay;
public Event() {
}
public Event(String name, Maturity maturity, Integer delay) {
super();
this.name = name;
this.maturity = maturity;
this.delay = delay;
}
public String getName() {
return name;
}
public Maturity getMaturity() {
return maturity;
}
public Integer getDelay() {
return delay;
}
@Override
public String toString() {
return "Event [name=" + name + ", maturity=" + maturity + ", delay="
+ delay + ", toString()=" + super.toString() + "]";
}
}
Element in the DB looks like:
Added: Event [name=Mercury, maturity=monthEnd, delay=0, toString()=bc.bacon.egg.data.local.pojos.events.Event@3b43dc25]
If I now insert a Element with a Index (Name) that already exists I get this Messages back:
To persist: Event [name=Mercury, maturity=monthEnd, delay=0, toString()=bc.bacon.egg.data.local.pojos.events.Event@7025bd08]
PersistanceHolder: bc.bacon.egg.data.local.PersistanceHolder@3d9d91bd
EntityManager: org.hibernate.jpa.internal.EntityManagerImpl@33cb2ee5
CRUDDao: bc.bacon.egg.data.local.dao.events.EventsDao@6073d6f3
Thread: Thread[qtp1055127737-17,5,main]
So a EntityExistsException is thrown and everything is fine.
But on the second run I get this:
To persist: Event [name=Mercury, maturity=monthEnd, delay=0, toString()=bc.bacon.egg.data.local.pojos.events.Event@700576a4]
PersistanceHolder: bc.bacon.egg.data.local.PersistanceHolder@3d9d91bd
EntityManager: org.hibernate.jpa.internal.EntityManagerImpl@33cb2ee5
CRUDDao: bc.bacon.egg.data.local.dao.events.EventsDao@6073d6f3
Thread: Thread[qtp1055127737-16,5,main]
Hibernate: insert into Event (delay, maturity, name) values (?, ?, ?)
Jun 18, 2015 3:10:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 23505, SQLState: 23505
Jun 18, 2015 3:10:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Eindeutiger Index oder Primärschlüssel verletzt: "PRIMARY_KEY_3 ON PUBLIC.EVENT(NAME) VALUES ('Mercury', 1)"
Unique index or primary key violation: "PRIMARY_KEY_3 ON PUBLIC.EVENT(NAME) VALUES ('Mercury', 1)"; SQL statement:
insert into Event (delay, maturity, name) values (?, ?, ?) [23505-187]
Jun 18, 2015 3:10:34 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Jun 18, 2015 3:10:34 PM com.vaadin.server.DefaultErrorHandler doDefault
Now my question, why is the first call okay and the duplication is found, but on the second run not? Okay I can catch the Exception which is thrown at the commit() function, but then, how could I remove the JDBC statements that remains?
Thanks, Josh
I got the answer. Here for everyone who gets the same Error:
Just add a merge operation before the persist operation.
PS: I don't know why this question got down votes, because its a legitimate question. The Javadoc of persist says:
It doesn't make it managed! It's trying to create it in the Database. So its first trying to persist it, then manage it. The Javadoc of merge says:
It sounds like overwriting, so I thought you can't use it here.