gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:05:10 27 4
gpt4 key购买 nike

这是我的员工类(class):

@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;
}
}

这是我的证书类(class)

@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 类

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");
}
}

我收到此错误

Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.hibernate.tutorials.hibernatetutorials.Certificate at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537) at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:165) at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:899) at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1308) at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at org.hibernate.tutorials.hibernatetutorials.ManageEmployee.main(ManageEmployee.java:44)

最佳答案

尝试下面的代码。

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

在持久化父对象的同时,需要指定级联类型来持久化子对象。

  • 级联

cascade (optional): specifies which operations should be cascaded from the parent object to the associated (child) object.

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

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

关于java - 如何解决对象引用未保存的 transient 实例的错误 - 在刷新之前保存 transient 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com