I have two entities Role and Grant with the following mapping:
public class Role extends BaseBean {
private static final long serialVersionUID = 1L;
private String name;
private Set<Grant> grants = new HashSet<Grant>();
// get set
}
public class Grant implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String data;
}
mapping orm:
<entity name="q2role" class="tn.waycon.alquasar2.adm.model.Role">
<attributes>
<basic name="name">
<column length="800" nullable="false" unique="true"/>
</basic>
<many-to-many name="grants" fetch="EAGER">
<join-table name="role_grant">
<join-column name="role_id"/>
<inverse-join-column name="grant_id"/>
</join-table>
<cascade>
<cascade-all/>
</cascade>
</many-to-many>
</attributes>
</entity>
<entity name="q2grant" class="tn.waycon.alquasar2.adm.model.Grant">
<attributes>
<id name="id">
<column name="id_g"/>
<generated-value stategy="IDENTITY" generator="SEQ_GEN"/>
</id>
<basic name="data"></basic>
</attributes>
</entity>
Now when i try to insert a new Role that contains an existing grants the transaction will fail because eclipselink is trying to insert the grants that already exist. Why eclipselink is doing this strange behaviour ? I am setting cascade-all and eclipselink must be smart enough to separate between cascade-persist and cascade-merge.
Main {
Role role = new Role();
List<Grant> grants = grantRepository.getGrantsBydata(List<String> datas);
role.setGrants(grants);
roleRepository.save(role);
}
The log:
WARNING [http-nio-8080-exec-2] org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke Processing of HttpInvokerServiceExporter remote call resulted in fatal exception tn.waycon.alquasar2.adm.service.api.IAdminService.createRole org.springframework.transaction.TransactionSystemException: Could not commit transaction JPA; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd) org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.BatchUpdateException: Violation of PRIMARY KEY "PK__Q2GRANT__9DB7D2FA15DA3E5D". Can not insert duplicate key in object 'dbo.Q2GRANT ". duplicate key value (13969). Error Code: 2627