- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 hibernate OneToOne 程序。它包含一个 导师类(class)和讲师详情 类(class)。
OneToOne 映射定义为从 Instructor 到 InstructorDetail。我只是保存了一个 Instructor 类的对象(由于级联,它也应该保存 InstructorDetail 对象)。场景是:
object references an unsaved transient instance - save the transient instance before flushing: 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();
}
}
最佳答案
最后我能够破解它。我们使用的 CascadeType 枚举是 javax.persistence 包。 session.save()
另一方面,方法是原始的 Hibernate 方法,不符合 JPA 规范,因此存在这种不一致。然而,如果我们使用 session.persist() 方法做同样的事情,它会像魅力一样工作。
关于java - hibernate :CasecadeType.ALL vs {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47666767/
我有一个简单的 hibernate OneToOne 程序。它包含一个 导师类(class)和讲师详情 类(class)。 OneToOne 映射定义为从 Instructor 到 Instructo
我是一名优秀的程序员,十分优秀!