detached entity passed to persist?
there is one to many relationship between User and Agreement, Now i want
to insert User into DB ,at the same time, the relate agreement data in DB
will update, but when i excute that i have occured a problem !
javax.persistence.PersistenceException:
org.hibernate.PersistentObjectException: detached entity passed to
persist: com.hna.adt.orm.Agreement
public void test_add() {
try {
DTOUser dto = new DTOUser();
dto.setAccount("account106eee0");
dto.setPassword("pwd106");
dto.setEmail("ui@qq.com");
List list2 = new ArrayList();
DTOAgreement dto6 = new DTOAgreement();
dto6 = agreementService.getAgreementById(17L);
list2.add(dto6);
dto.getDtoAgreements().add(dto6);
service.add(dto);
} catch (Exception e) {
System.err.println(e);
}
}
one entity User:
@Entity
@Table(name="adt_user")
public class User {
private long id;
......
private Set<Agreement> agreements = new HashSet<Agreement>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy =
"applyer")
public Set<Agreement> getAgreements() {
return agreements;
}
public void setAgreements(Set<Agreement> agreements) {
this.agreements = agreements;
}
}
other entity: agreement.java
public class Agreement implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = -8858462668175007383L;
private long id;
private User applyer;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "agreement_applyer")
public User getApplyer() {
return applyer;
}
public void setApplyer(User applyer) {
this.applyer = applyer;
}
}
No comments:
Post a Comment