如何解决对象引用未保存的瞬态实例的此错误 - 在刷新之前保存瞬态实例?

时间:2015-10-09 12:22:51

标签: java oracle hibernate

这是我的员工类:

@Entity
public class Employee {
@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
private int salary;
@OneToMany(targetEntity = Certificate.class)
private Set<?> certificate;
public Employee() {}
public Employee(String fname, String lname, int salary, Set<?> certificate) {
    this.firstName = fname;
    this.lastName = lname;
    this.salary = salary;
    this.certificate = certificate;
}
public Employee(Employee e1) {}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String first_name) {
    this.firstName = first_name;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String last_name) {
    this.lastName = last_name;
}
public int getSalary() {
    return salary;
}
public void setSalary(int salary) {
    this.salary = salary;
}
public Set<?> getCertificate() {
    return certificate;
}
public void setCertificate(Set<?> certificate) {
    this.certificate = certificate;
}
}

这是我的证书类

@Entity
public class Certificate {
@Id
@GeneratedValue
private int id;
private String name;
public Certificate() {}
public Certificate(String name) {
    this.name = name;
}
public void setId(int id) {
    this.id = id;
}
public int getId() {
    return id;
}
public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (!this.getClass().equals(obj.getClass())) {
        return false;
    }
    Certificate obj2 = (Certificate) obj;
    if ((this.id == obj2.getId()) && (this.name.equals(obj2.getName()))) {
        return true;
    }
    return false;
}
public int hashCode() {
    int tmp = 0;
    tmp = (id + name).hashCode();
    return tmp;
}
}

这是我的 ManageEmployee Class

public class ManageEmployee {

public static void main(String[] args) {
    Employee e1 = new Employee();
    Employee e2 = new Employee();

    e1.setFirstName("deepak");
    e1.setLastName("SP");
    e1.setSalary(1000);
    Set<Certificate> set1 = new HashSet<Certificate>();
    set1.add(new Certificate("BE"));
    set1.add(new Certificate("MS"));
    e1.setCertificate(set1);

    e2.setFirstName("deepaksp");
    e2.setLastName("SP");
    e2.setSalary(10010);
    Set<Certificate> set2 = new HashSet<Certificate>();
    set2.add(new Certificate("BE"));
    set2.add(new Certificate("MS"));
    e2.setCertificate(set2);

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(
            new StandardServiceRegistryBuilder().applySettings(new Configuration().configure().getProperties())
                    .build());
    System.out.println("Session Factory built");
    Session session = sessionFactory.openSession();
    System.out.println("Session opened");
    session.beginTransaction();
    System.out.println("Transaction has begun");
    session.save(e1);
    session.save(e2);
    System.out.println("Object has been saved");
    session.getTransaction().commit();
    System.out.println("transaction has been commited");
}
}

我收到此错误

  

线程“main”中的异常org.hibernate.TransientObjectException:   object引用未保存的瞬态实例 - 保存瞬态   冲洗前的实例:   org.hibernate.tutorials.hibernatetutorials.Certificate at   org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)   在org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)at   org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:165)   在   org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:899)   在   org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1308)   在   org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)   在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)   在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)   在   org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)   在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)   在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)at   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)   在   org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)   在   org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)   在   org.hibernate.tutorials.hibernatetutorials.ManageEmployee.main(ManageEmployee.java:44)

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。

@OneToMany(targetEntity = Certificate.class, cascade=CascadeType.ALL)
private Set<?> certificate;

您需要指定Cascade类型以在保留父对象时保留子对象。

  • 级联
  

cascade(可选):指定应该级联哪些操作   相关(子)对象的父对象。

Allowed values: cascade={ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH}.

请参阅oracle文档以获取有关允许值类型的更多详细信息。 https://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html