In a Spring MVC application using hibernate and jpa through a MySQL database, I get the following error message for a child when I try to save a parent that includes a child:
Duplicate entry 'string1-string2' for key 'PRIMARY'
Here, string1they string2relate to two parts of the composite primary key of the child object. How to fix this error?
Here's how to define the relationship between entities in the parent Address:
@ManyToOne(cascade = { CascadeType.ALL }, fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name = "usecode", referencedColumnName = "code", insertable = false, updatable = false),
@JoinColumn(name = "usecodesystem", referencedColumnName = "codesystem", insertable = false, updatable = false)
})
public HL7GeneralCode use;
Here's how to define a relation in a child GeneralCode:
@OneToMany(mappedBy = "use", cascade = {CascadeType.ALL})
private Set<HL7Address> addresses;
The full stack trace can be viewed by clicking on this link .
The full code for the object Addresscan be found at this link .
GeneralCode .
.
BaseEntity, Address, .
. , , .
EDIT:
:
@Override
public void savehl7Address(HL7Address addr) {
if ((Integer)addr.getId() == null) {
System.out.println("[[[[[[[[[[[[ about to persist address ]]]]]]]]]]]]]]]]]]]]");
this.em.persist(addr);}
else {
System.out.println("]]]]]]]]]]]]]]]]]] about to merge address [[[[[[[[[[[[[[[[[[[[[");
this.em.merge(addr);}
}
:
@Ben75, this.em.persist(addr.getUse());. , if , if if(addr.getUse() != null && addr.getId()==null). .
@Override
public void savehl7Address(HL7Address addr) {
if(addr.getUse() != null && addr.getId()==null){
System.out.println("about to this.em.persist(addr.getUse());");
this.em.persist(addr.getUse());
return;
}
System.out.println("=========================== inside jpahl7patientrespository.savehl7Address(addr)");
if ((Integer)addr.getId() == null) {
System.out.println("[[[[[[[[[[[[ about to persist address ]]]]]]]]]]]]]]]]]]]]");
this.em.persist(addr);}
else {
System.out.println("]]]]]]]]]]]]]]]]]] about to merge address [[[[[[[[[[[[[[[[[[[[[");
this.em.merge(addr);}
}
HL7Address :
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name = "usecode", referencedColumnName = "code", insertable = false, updatable = false),
@JoinColumn(name = "usecodesystem", referencedColumnName = "codesystem", insertable = false, updatable = false)
})
public HL7GeneralCode use;
HL7GeneralCode :
@OneToMany(mappedBy = "use")
private Set<HL7Address> addresses;
.
?
:
ben75, :
if(addr.getUse() != null && !this.em.contains(addr.getUse())){
System.out.println("about to this.em.persist(addr.getUse());");
this.em.persist(addr.getUse());return;
}
, , , SYSO , .
.