【发布时间】:2011-10-27 12:33:38
【问题描述】:
我在执行以下场景时遇到了上述异常。
Students 和 Address 具有 Many-To-One 关系,而 Student 和 PhoneNumbers 具有 One-To-Many 关系。在 EntityManager 对象上调用 persist 方法以保存“学生”对象时,出现以下异常:
org.hibernate.TransientObjectException: 对象引用了一个未保存的 瞬态实例 - 在刷新之前保存瞬态实例: model.Students1.addressId -> model.Address
可以采取哪些措施来解决它?
详情如下:
DAO 类:
public class DAO {
public static void main(String[] arr){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToManyPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tr= em.getTransaction();
try{
tr.begin();
PhoneNumbers p1 = new PhoneNumbers();
PhoneNumbers p2 = new PhoneNumbers();
p1.setPhoneType("mobile");
p1.setPhoneNo("9881592106");
p2.setPhoneType("landline");
p2.setPhoneNo("24214988");
Set<PhoneNumbers> phones = new HashSet<PhoneNumbers>();
phones.add(p1);
phones.add(p2);
em.persist(p1);
em.persist(p2);
Address a1 = new Address();
a1.setCity("Pune");
a1.setZip("400987");
Students1 s1 = new Students1();
s1.setName("Alka");
s1.setAddressId(a1);
s1.setPhoneNo(phones);
em.persist(s1);
tr.commit();
}
catch(Exception e){
e.printStackTrace();
}
finally{
emf.close();
}
}
}
学生1班:
@Entity
@Table(name = "STUDENTS")
public class Students1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ID")
@ManyToOne
private Address addressId;
@OneToMany(cascade ={CascadeType.MERGE,CascadeType.PERSIST})
@JoinTable(name="STUDENT_PHONE",joinColumns={@JoinColumn(name="STUDENTS.ID")},inverseJoinColumns={@JoinColumn(name="PHONENUMBERS.ID")})
private Set<PhoneNumbers> phoneNo = new HashSet<PhoneNumbers>();
public void setPhoneNo(Set<PhoneNumbers> phoneNo) {
this.phoneNo = phoneNo;
}
public Set<PhoneNumbers> getPhoneNo() {
return phoneNo;
}
public Students1() {
}
public Students1(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddressId() {
return addressId;
}
public void setAddressId(Address addressId) {
this.addressId = addressId;
}
}
地址类
@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;
@Column(name = "CITY")
private String city;
@Column(name = "ZIP")
private String zip;
@OneToMany(mappedBy = "addressId")
private Collection<Students1> students1Collection;
public Address() {
}
public Address(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Collection<Students1> getStudents1Collection() {
return students1Collection;
}
public void setStudents1Collection(Collection<Students1> students1Collection) {
this.students1Collection = students1Collection;
}
}
电话号码类
@Entity
public class PhoneNumbers implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="PhoneNo")
private String phoneNo;
@Column(name="PhoneType")
private String phoneType;
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getPhoneType() {
return phoneType;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
【问题讨论】:
标签: hibernate jakarta-ee jpa