我试图使用注释在Person类和Address类之间实现hibernate一对一双向映射。在运行程序时,它给了我一些错误
org.hibernate.TransientObjectException: object references an unsaved transient
instance - save the transient instance before flushing: com.vaannila.domain.Person
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3123)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.vaannila.domain.Add.main(Add.java:58)
以下是我的课程
Person.java
package com.vaannila.domain;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
private long personId;
private String personName;
private Address personAddress;
private Set<Phone> personPhoneNumbers = new HashSet<Phone>(0);
public Person() {
}
public Person(String personName, Address personAddress, Set<Phone> personPhoneNumbers) {
this.personName = personName;
this.personAddress = personAddress;
this.personPhoneNumbers = personPhoneNumbers;
}
@Id
@GeneratedValue
@Column(name = "PERSON_ID")
public long getPersonId() {
return this.personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
@Column(name = "PERSON_NAME", nullable = false, length = 100)
public String getPersonName() {
return this.personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
@OneToOne(cascade = CascadeType.ALL)
public Address getPersonAddress() {
return this.personAddress;
}
public void setPersonAddress(Address personAddress) {
this.personAddress = personAddress;
}
@OneToMany
@JoinTable(name = "PERSON_PHONE", joinColumns =
{ @JoinColumn(name = "PERSON_ID") }, inverseJoinColumns =
{ @JoinColumn(name = "PHONE_ID") })
public Set<Phone> getPersonPhoneNumbers() {
return personPhoneNumbers;
}
public void setPersonPhoneNumbers(Set<Phone> personPhoneNumbers) {
this.personPhoneNumbers = personPhoneNumbers;
}
}
Addres.java
package com.vaannila.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {
private long addressId;
private String street;
private String city;
private String state;
private String zipcode;
private Person person;
public Address() {
}
@Id
@GeneratedValue
@Column(name = "ADDRESS_ID")
public long getAddressId() {
return this.addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
@Column(name = "ADDRESS_STREET", nullable = false, length=250)
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
@Column(name = "ADDRESS_CITY", nullable = false, length=50)
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name = "ADDRESS_STATE", nullable = false, length=50)
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
@Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10)
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
@OneToOne
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Phone.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.vaannila.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author junaid
*/
@Entity
@Table(name = "PHONE")
public class Phone implements java.io.Serializable {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {
}
public Phone(String phoneType, String phoneNumber) {
this.phoneType = phoneType;
this.phoneNumber = phoneNumber;
}
@Id
@GeneratedValue
@Column(name = "PHONE_ID")
public long getPhoneId() {
return this.phoneId;
}
public void setPhoneId(long phoneId) {
this.phoneId = phoneId;
}
@Column(name = "PHONE_TYPE", nullable = false, length=10)
public String getPhoneType() {
return this.phoneType;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
@Column(name = "PHONE_NUMBER", nullable = false, length=15)
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
主要课程
package com.vaannila.domain;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.vaannila.util.HibernateUtil;
import java.util.HashSet;
import java.util.Set;
public class Add {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
//Transaction for person 1 start
Address address1 = new Address();
address1.setCity("Bangalore");
address1.setState("Karnataka");
address1.setStreet("Ring Road");
address1.setZipcode("560000");
Set<Phone> phoneNumbers = new HashSet<Phone>();
phoneNumbers.add(new Phone("house","32354353"));
phoneNumbers.add(new Phone("mobile","9889343423"));
Person person = new Person();
person.setPersonName("Junaid");
person.setPersonAddress(address1);
person.setPersonPhoneNumbers(phoneNumbers);
session.save(person);
//Transaction for person 1 end
//Transaction for person 2 start
Set<Phone> phoneNumbers2 = new HashSet<Phone>();
phoneNumbers2.add(new Phone("house","32354353"));
phoneNumbers2.add(new Phone("mobile","9889343423"));
Person person1 = new Person();
person1.setPersonName("Wayne");
person1.setPersonPhoneNumbers(phoneNumbers2);
Address address2 = new Address();
address2.setCity("Chennai");
address2.setState("Tamil Nadu");
address2.setStreet("OMR Road");
address2.setZipcode("4000");
address2.setPerson(person1);
session.save(address2);
//Transaction for person 2 end
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
正在创建所有表,但删除表时,除了Person和Address之外的所有表都不会被删除。
有人可以帮助我吗?
提前致谢。
答案 0 :(得分:0)
有工作的人。
我错过了mappedBy属性。加上它的工作正常。
@OneToOne(mapperdBy="personAddress")
public Person getPerson() {
return person;
}