JPA双向@ManyToMany关系:赋值导致两个插入

时间:2015-01-27 18:55:46

标签: java hibernate jpa many-to-many

我正在基于JavaEE + JPA的JBoss服务器上构建Rest-API。

我有两个名为" Mitglied"和" Einsatz":

@Entity
@Table(name="MITGLIEDER")
public class Mitglied  implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    // bunch of more attributes

    @ManyToMany(mappedBy="teilnehmendeMitglieder")
    private Set<Einsatz> einsaetze;


    public Set<Einsatz> getEinsaetze() {
        return einsaetze;
    }
    public void setEinsaetze(Set<Einsatz> einsaetze) {
        this.einsaetze = einsaetze;
    }

    public void addEinsatz(Einsatz e) {
        addEinsatz(e, true);
    }

    public void addEinsatz(Einsatz b, boolean set) {
        if (einsaetze == null) {
            einsaetze = new HashSet<Einsatz>();
        }
        if (b != null) {
            getEinsaetze().add(b);
            if (set) {
                b.addMitglied(this, false);
            }
        }
    }
    public void removeEinsatz(Einsatz e) {
        removeEinsatz(e, true);
    }
    public void removeEinsatz(Einsatz e, boolean cascade) {
        getErworbeneDienstgrade().remove(e);
        if (cascade) {
            e.removeMitglied(this, false);
        }
    }

    // more getters and setters
}

这是&#34; Einsatz&#34;:

@Entity
@Table(name="EINSAETZE")

public class Einsatz implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    // some more attributes

    @ManyToMany
    @JoinTable(name = "MITGLIED_HAT_AN_EINSATZ_TEILGENOMMEN",
      joinColumns = {@JoinColumn( name = "EINSATZ_ID", referencedColumnName = "ID" )}, // ID ist die Id von Einsatz
      inverseJoinColumns = {@JoinColumn( name = "MITGLIED_ID", referencedColumnName = "ID" )} // ID ist die Id von Mitglied
    )
    private Set<Mitglied> teilnehmendeMitglieder;

    private static final long serialVersionUID = 1L;

    public Einsatz() {
        super();
    }   
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }   

    public void removeMitglied(Mitglied m) {
        removeMitglied(m, true);
    }

    public void removeMitglied(Mitglied m, boolean cascade) {
        if (teilnehmendeMitglieder!= null && teilnehmendeMitglieder.contains(m)) {
            teilnehmendeMitglieder.remove(m);
        }
        if (cascade) {
            m.removeEinsatz(this, false);
        }
    }

    public void addMitglied(Mitglied m) {
        addMitglied(m, true);
    }

    public void addMitglied(Mitglied b, boolean set) {
        if (teilnehmendeMitglieder == null) {
            teilnehmendeMitglieder = new HashSet<Mitglied>();
        }
        if (b != null) {
            getTeilnehmendeMitglieder().add(b);
            if (set) {
                b.addEinsatz(this, false);
            }
        }
    }
    // more getters and setters       
}

这是我的问题:

在尝试将Mitglied分配给Einsatz时,Hibernate试图将关系两次插入到连接表中(&#34; MITGLIED_HAT_AN_EINSATZ_TEILGENOMMEN&#34;),这会导致违反约束。

我尝试了各种级联类型和星座,例如作为关系所有者,没有任何对我有用的东西。搜索SO给了我一些提示,即使没有解决问题。我到目前为止,至少第一条记录被写入MySQL-DB,因为我不断获得ConstraintViolationException ...

我确定,这是非常愚蠢的事情,然后我再也看不到重点了。我究竟做错了什么?我得到的只是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '39-1' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_21]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_21]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_21]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) [rt.jar:1.7.0_21]
    at [... and so on ...]

以下是导致此错误的业务逻辑:

public void assignMitgliedToEinsatz(int mitgliedId, int einsatzId){
    Mitglied m = mitgliederDao.getMitglied(mitgliedId);
    Einsatz e = dao.readIt(einsatzId, Einsatz.class);
    e.addMitglied(m);
    dao.updateIt(e); // calls JPAs merge() method
}

编辑:

我尝试了Kikin-Sama的设置场到瞬态的建议,然而错误仍然存​​在。这里有一些hibernate-logs的详细错误:

21:07:19,544 INFO  [stdout] (default task-8) Hibernate: 
21:07:19,545 INFO  [stdout] (default task-8)     insert 
21:07:19,545 INFO  [stdout] (default task-8)     into
21:07:19,545 INFO  [stdout] (default task-8)         MITGLIED_HAT_AN_EINSATZ_TEILGENOMMEN
21:07:19,545 INFO  [stdout] (default task-8)         (EINSATZ_ID, MITGLIED_ID) 
21:07:19,545 INFO  [stdout] (default task-8)     values
21:07:19,545 INFO  [stdout] (default task-8)         (?, ?)
21:07:19,545 INFO  [stdout] (default task-9) Hibernate: 
21:07:19,545 INFO  [stdout] (default task-9)     insert 
21:07:19,545 INFO  [stdout] (default task-9)     into
21:07:19,545 INFO  [stdout] (default task-9)         MITGLIED_HAT_AN_EINSATZ_TEILGENOMMEN
21:07:19,545 INFO  [stdout] (default task-9)         (EINSATZ_ID, MITGLIED_ID) 
21:07:19,545 INFO  [stdout] (default task-9)     values
21:07:19,545 INFO  [stdout] (default task-9)         (?, ?)
21:07:19,547 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-9) SQL Error: 1062, SQLState: 23000
21:07:19,548 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-9) Duplicate entry '99-1' for key 'PRIMARY'
21:07:19,549 INFO  [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] (default task-9) HHH000010: On release of batch it still contained JDBC statements
21:07:19,550 WARN  [com.arjuna.ats.arjuna] (default task-9) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff7f000101:497b6818:54c7efdd:bf, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@d5d8750 >: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1882) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:115) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:50) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:358) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:91) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1166) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.endTransaction(TransactionalInterceptorBase.java:147) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:93) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:52) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_21]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_21]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_21]
    at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_21]
    at org.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNext(AbstractInterceptionChain.java:103) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNextInterceptor(AbstractInterceptionChain.java:81) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:48) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:41) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:53) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
    at de.florian.remote.mitglieder.EinsaetzeRemote$Proxy$_$$_WeldSubclass.assignEinsatzToMitglied(Unknown Source) [classes:]
    at de.florian.remote.mitglieder.EinsaetzeRemote$Proxy$_$$_WeldClientProxy.assignEinsatzToMitglied(Unknown Source) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_21]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_21]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_21]
    at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_21]
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:384) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:342) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297) [jersey-common-2.13.jar:]
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030) [jersey-server-2.13.jar:]
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373) [jersey-container-servlet-core-2.13.jar:]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) [jersey-container-servlet-core-2.13.jar:]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) [jersey-container-servlet-core-2.13.jar:]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) [jersey-container-servlet-core-2.13.jar:]
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_21]
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1531) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.action.internal.CollectionUpdateAction.execute(CollectionUpdateAction.java:102) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:110) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    ... 74 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '99-1' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_21]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_21]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_21]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) [rt.jar:1.7.0_21]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jdbc.Util.getInstance(Util.java:383)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4140)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
    at org.jboss.jca.adapters.jdbc.CachedPreparedStatement.executeUpdate(CachedPreparedStatement.java:119)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    ... 84 more

2 个答案:

答案 0 :(得分:2)

好的,明白了! 一些对SO的拼命研究让我想到了这个问题:

JPA many-to-many persist to join table (参见Ravi Thapliyal的回答)

简而言之,我必须将cascade = CascadeType.PERSIST添加到关系的Mitglied一侧,所以这就是我的情况:

// Code from class "Mitglied" and "Einsatz"same as in Question, 
// but this annotation in Mitglied:
@ManyToMany(mappedBy="teilnehmendeMitglieder", cascade=CascadeType.PERSIST)
private Set<Einsatz> einsaetze;

答案 1 :(得分:0)

这是一个非常常见的问题,如果您映射@ManyToMany关系的两侧,Hibernate将尝试为每个注释执行插入。

我建议您更改代码:

@ManyToMany(mappedBy="teilnehmendeMitglieder")
private Set<Einsatz> einsaetze;

类似于:

private transient Set<Einsatz> einsaetze;

那应该可以解决你的问题。显然,如果你想为给定的Mitglied检索所有Einsatz,你将需要一个单独的查询。

干杯。