Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate join tables duplicate entry - Could not execute JDBC batch update
    primarykey
    data
    text
    <p>I have a simple domain class Licensee:</p> <pre><code>@Entity @Table(name = "LICENSEE") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Licensee implements Serializable, Comparable { private Integer id; private String orgName; /* ... */ private Set&lt;Address&gt; address = new HashSet&lt;Address&gt;(); @Id @Column(name = "ID") @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "org.hibernate.id.IncrementGenerator") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "ORGNAME") public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } /* ... */ @OneToMany @Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) @JoinTable(name = "LICENSEEADDRESS", joinColumns = { @JoinColumn(name = "LICENSEEID", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "ADDRESSID") } ) public Set&lt;Address&gt; getAddress() { return address; } public void setAddress(Set&lt;Address&gt; address) { this.address = address; } public void addAddress(Address address) { this.address.add(address); } } </code></pre> <p>which holds a reference to a Set of Address objects. I'm using the @JoinTable annotation for the db relation between the two classes. I have hibernate.hbm2ddl set to create which creates the join table LICENSEEADDRESS with a composite key consisting of the LICENSEEID and the ADDRESSID as expected. However, when I call saveOrUpdate on a Licensee object with more than one Address object in the Set at a time, I get this:</p> <pre><code>java.sql.BatchUpdateException: Duplicate entry '1' for key 2 com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647) org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171) org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027) org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365) org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655) org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732) org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701) org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321) org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) $Proxy18.saveLicensee(Unknown Source) net.grouplink.gilf.web.controllers.LicenseeFormController.onSubmit(LicenseeFormController.java:64) org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267) org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265) org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153) org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511) javax.servlet.http.HttpServlet.service(HttpServlet.java:710) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) </code></pre> <p>So the second query in the batch isn't completed because it thinks there is a duplicate key, which is true for that one column. But with a composite key, wouldn't both ids need to be the same to cause a constraint violation? What am I missing here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload