Hi, ich sitze nun seit diversen Stunden an einem Problem und weiß mir nicht mehr zu helfen. Ich habe noch nicht viel Erfahrung mit JPA, aber bin erstmals drauf umgestiegen, da es ja eigentlich Arbeit abnehmen sollte.. dabei wäre ich ohne vermutlich schon fertig -_-
Naja, ich habe eine sehr einfache Struktur, in der ich eine Entity persistiere, die mit ihrer Persistierung ein weiteres Objekt in einer anderen Tabelle speichert. Also CascadeType.PERSIST. Wenn ich das Objekt später wieder löschen will, klappt es nicht, wegen folgender Fehlermeldung:
Meine Entities sind wie folgt definiert:
EDIT:
Hier noch meine Delete-Methode:
Naja, ich habe eine sehr einfache Struktur, in der ich eine Entity persistiere, die mit ihrer Persistierung ein weiteres Objekt in einer anderen Tabelle speichert. Also CascadeType.PERSIST. Wenn ich das Objekt später wieder löschen will, klappt es nicht, wegen folgender Fehlermeldung:
Code:
java.lang.RuntimeException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
at de.gold.database.DatabaseManager.deleteEntityObject(DatabaseManager.java:71)
at de.gold.database.DatabaseManagerTest._2_persistAdder(DatabaseManagerTest.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1338)
at de.gold.database.DatabaseManager.deleteEntityObject(DatabaseManager.java:67)
... 28 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
... 29 more
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; FK_3UNEX2ESC8THC4VPSXFJTW1M0 table: STRINGFIX_ADDER
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 39 more
Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no action; FK_3UNEX2ESC8THC4VPSXFJTW1M0 table: STRINGFIX_ADDER
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.StatementDML.performReferentialActions(Unknown Source)
at org.hsqldb.StatementDML.delete(Unknown Source)
at org.hsqldb.StatementDML.executeDeleteStatement(Unknown Source)
at org.hsqldb.StatementDML.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 42 more
Meine Entities sind wie folgt definiert:
Code:
@Entity
public class StringFix {
@Id
private String hashcode;
@NotNull
private String match;
private boolean isRegex;
private boolean toUpper;
private boolean toLower;
...
Code:
@Entity
public class Adder {
@Id
private String hashcode;
@NotNull
@OneToOne(orphanRemoval = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinTable(
name="STRINGFIX_ADDER",
joinColumns=
@JoinColumn(name="STRINGFIX_HASH", referencedColumnName="HASHCODE", nullable = false, updatable = false),
inverseJoinColumns=
@JoinColumn(name="ADDER_HASH", referencedColumnName="HASHCODE", nullable = false, updatable = false))
private StringFix regexMatch;
@NotNull
private String prefix;
@NotNull
private String suffix;
@Transient
private Pattern pattern;
@Transient
private String lastRegexMatch;
@Transient
private String runBy;
@Transient
private String ignored;
EDIT:
Hier noch meine Delete-Methode:
Code:
public <T> void deleteEntityObject(T entityObject) {
EntityManager em = factory.createEntityManager();
try {
em.getTransaction().begin();
Object entity = em.find(entityObject.getClass(), String.valueOf(entityObject.hashCode()));
em.remove(entity);
em.flush();
em.getTransaction().commit();
} catch (Throwable throwable) {
em.getTransaction().rollback();
throw new RuntimeException(throwable);
} finally {
em.close();
}
}
Zuletzt bearbeitet: