当我尝试运行以下代码时,我得到org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: safu.Publisher
异常:
public class SafuClient {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Publisher publisher = new Publisher("ABC", "ABC Co.");
Book book = new Book("1-932394-88-5", "Safu", publisher);
session.save(book);
session.getTransaction().commit();
}
}
我有一个从Book到Publisher的多对一关系。Book和Publisher实体如下:
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String isbn;
private String name;
@ManyToOne(cascade={CascadeType.PERSIST})
@JoinColumn(name="publisher_id")
private Publisher publisher;
public Book() {}
public Book(String isbn, String name, Publisher publisher) {
this.isbn = isbn;
this.name = name;
this.publisher = publisher;
}
}
@Entity
public class Publisher {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String code;
private String name;
public Publisher() {}
public Publisher(String code, String name) {
this.code = code;
this.name = name;
}
}
如果我用Book
实体中的@ManyToOne(cascade={CascadeType.ALL})
替换@ManyToOne(cascade={CascadeType.PERSIST})
,一切都工作得很好。
有人能解释一下为什么会这样吗?
尝试session.persist(book);
来解决问题,而不是session.save(book);
session.save(book)
方法的工作原理是从Book
实体中的@ManyToOne
注释中删除级联属性(cascade={CascadeType.PERSIST}
),并在publisher字段中添加@Cascade (value=CascadeType.SAVE_UPDATE)
。
@Cascade (value=CascadeType.SAVE_UPDATE)
使用Hibernate的本地注解。
遵循更新后的Book.java:
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String isbn;
private String name;
@ManyToOne
@JoinColumn(name="publisher_id")
@Cascade(value=CascadeType.SAVE_UPDATE)
private Publisher publisher;
public Book() {}
public Book(String isbn, String name) {
this.isbn = isbn;
this.name = name;
}
public Book(String isbn, String name, Publisher publisher) {
this.isbn = isbn;
this.name = name;
this.publisher = publisher;
}
}