Hibernate的一个问题object references an unsaved transient instance - save the transi5

1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着视频教程里的例子一步一步做的,但视频里却没有异常,是什么原因?)

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.bjsxt.hibernate.model.Group

2.如果在测试程序中 HibernateORMappingTest.java:在保存User之前,先把Group也保存了,即加上这一句s.save(g); 就不会出现异常了。可是视频教程里没有这一句,只是设置Cascade级联, 却能通过,这是什么原因呢。

User.java:
  @Entity
  @Table(name="t_user")
  public class User {
private int id;
private String name;
private Group group; 

@ManyToOne(cascade={CascadeType.ALL} ) //Cascade设置级联
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
  } 

Group.java
  @Entity
  @Table(name ="t_group")
  public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} 

@OneToMany(mappedBy="group")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
  } 

测试程序 HibernateORMappingTest.java:
  public class HibernateORMappingTest { 

private static SessionFactory sessionFactory;
@BeforeClass
public static void beforeClass(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
sessionFactory= new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass(){
sessionFactory.close();
}
@Test
public void testSaveUser(){
User u = new User();
u.setName("u1");
Group g = new Group();
g.setName("g1");
u.setGroup(g);
Session s= sessionFactory.getCurrentSession();
s.beginTransaction();
//s.save(g); 加上这一句,就不报错了,可是视频教程里没这一句,只是设置了级联Cascade却能通过
s.save(u);
s.getTransaction().commit();
}
  @Test
public void testSchemaExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
public static void main(String[] args){
beforeClass();
}
} 
时间: 2024-08-10 21:27:49

Hibernate的一个问题object references an unsaved transient instance - save the transi5的相关文章

ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom

本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.mao.Address 关于这个异常应该是Hibernate初学者经常遇到的,导致

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.zs.entity.GuahaodanEntity; nested excepti

ssh里hibernate的 一对多多对一的问题  save时 一的一方还处于持久化状态时,多的一方这时候保存的话就会出现这个问题 必须先将一的一方先保存解除持久化状态,才可以再保存多的一方 注意保存顺序就好了 原文地址:https://www.cnblogs.com/King-Jin/p/10961305.html

object references an unsaved transient instance - save the transient instance before flushing: com.jspxcms.core.domain.ScTeam

还有个细节   当前台传到后台的类中如果有为空的  如id为null  或者对象整个为空的   也也会请发这个异常

object references an unsaved transient instance【异常】

[异常提示] TransientObjectException: object references an unsaved transient instance -save the transient instance before flushing: com.jspxcms.core.domain.ScTeam [网上参考资料] [参考资料二] 标题:   object references an unsaved transient instance - save the transient

三大框架常遇的错误:hibernate : object references an unsaved transient instance

hibernate : object references an unsaved transient instance 该错误是操作顺序的问题,例如: save或update顺序问题---比如学生表和班级表..学生表里有班级ID的外键.一对多的关系.你save的时候应该先save班级,再save学生..否则学生的外键没有对应的值,引发异常.

ManyToMany【项目随笔】关于异常object references an unsaved transient instance

在保存ManyToMany  时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance 如图: 出现原因:单向ManyToMany保存顺序反了,应当先保存主控端permission对象 错误代码: @Transactional(readOnly=f

org.unsaved transient instance - save the transient instance before flushing: bug解决方案

最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instance before flushing: org.blog.blog.domain.HighStudent 解释:意思是说hibernate从数据库中查询出来的一个对象HighSchool(为持态,我给它的事物定义为readOnly,只有只读权限),然后又给它的属性或者它属性的属性(highStude

Hibernate 第一个Hibernate应用程序

Chapter 1. Tutorial   第一章 使用说明书 Table of Contents   目录 1.1. Part 1 - The first Hibernate Application   第一节 第一个Hibernate应用程序 1.1.1. Setup   设置开发环境 1.1.2. The first class  创建第一个类 1.1.3. The mapping file  创建类与数据库表的映射文件 1.1.4. Hibernate configuration  Hi

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错

今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:-- 找其原因是因为同一个object,如一个person在seession里保存了一份,而增加的别的object,如company的时候,由于做了关联关系,从数据库里又get了一个person,而这个person和s