我有一个简单的hibernate OneToOne程序。它由讲师类和 InstructorDetail 类组成。 OneToOne映射是从Instructor到InstructorDetail定义的。我只是保存一个Instructor类的对象(由于级联,它也应该保存InstructorDetail对象)。场景是:
当我使用CascadeType.ALL时,它工作正常。
但是,当我使用包含所有可能的级联类型的数组时( @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH,CascadeType.REMOVE}) ),它给了我以下错误消息:
对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.poc.entity.InstructorDetail
请在下面找到我的代码:
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="instructor")
public class Instructor {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@OneToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE})
@JoinColumn(name="instructor_detail_id")
private InstructorDetail instructorDetail;
public Instructor() {
}
public Instructor(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public InstructorDetail getInstructorDetail() {
return instructorDetail;
}
public void setInstructorDetail(InstructorDetail instructorDetail) {
this.instructorDetail = instructorDetail;
}
}
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="youtube_channel", length=40)
private String youtubeChannel;
@Column(name="hobby", length=20)
private String hobby;
public InstructorDetail() {
}
public InstructorDetail(String youtubeChannel, String hobby) {
super();
this.youtubeChannel = youtubeChannel;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getYoutubeChannel() {
return youtubeChannel;
}
public void setYoutubeChannel(String youtubeChannel) {
this.youtubeChannel = youtubeChannel;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
...
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.poc.entity.Instructor;
import com.poc.entity.InstructorDetail;
public class OneToOneDemo {
private static SessionFactory sessionFactory;
static {
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.buildSessionFactory();
}
public static void main(String...s) {
InstructorDetail instructorDetail = new InstructorDetail("xyz/youtube.com","cooking");
Instructor instructor = new Instructor("Bhupesh","Nimje");
try {
instructor.setInstructorDetail(instructorDetail);
save(instructor);
}
finally {
sessionFactory.close();
}
}
public static void save(Instructor instructor) {
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.save(instructor);
session.getTransaction().commit();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
session.close();
}
}
}
答案 0 :(得分:2)
最后我能破解它。我们使用的CascadeType枚举是javax.persistence包。另一方面,session.save()
方法是一种原始的Hibernate方法,它不符合JPA规范,因此不一致。但是,如果我们使用session.persist()方法做同样的事情,它将像魅力一样工作。