I have a simple domain class Licensee:
@Entity
@Table(name = "LICENSEE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Licensee implements Serializable, Comparable {
private Integer id;
private String orgName;
/* ... */
private Set<Address> address = new HashSet<Address>();
@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<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
public void addAddress(Address address) {
this.address.add(address);
}
}
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:
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)
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?