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

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

  1. 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.TransientOb ...

  2. object references an unsaved transient instance save the transient instance before flushing

    object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...

  3. object references an unsaved transient instance - save the transient instance before flushing异常问题处理

    一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...

  4. 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 instance before flushing: com.j ...

  5. object references an unsaved transient instance - save the transient instance before flushing错误

    异常1:not-null property references a null or transient value解决方法:将“一对多”关系中的“一”方,not-null设置为false(参考资料: ...

  6. hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing

    我的问题出在,删除的对象对应的表中有一个外键,关联着另外一个表,可是另外一个表中没有数据,所以报了这个错误. 参考http://www.cnblogs.com/onlywujun/archive/20 ...

  7. object references an unsaved transient instance【异常】

    [异常提示] TransientObjectException: object references an unsaved transient instance -save the transient ...

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

    hibernate : object references an unsaved transient instance 该错误是操作顺序的问题,比如: save或update顺序问题---比方学生表和 ...

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

    在保存ManyToMany  时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.Tran ...

随机推荐

  1. Python——递归、二分查找算法

    递归函数 1. 递归 (1)什么是递归:在函数中调用自身函数(2)最大递归深度:默认997/998——是Python从内存角度出发做的限制 n = 0 def story(): global n n+ ...

  2. AI(四): 微信与luis结合(下)

    LUIS(Language Understanding Intelligent Services)是微软新近推出了的的语义理解服务,可以方便用户进行API调用,创建自己场景的语义理解服务,网址为 ht ...

  3. Windows Server 2008 R2 3389端口更改

    Windows Server 2008 R2 3389端口更改 2016-04-28 23:08 4734人阅读 评论(0) 收藏 举报  分类: Windows(61)  版权声明:本文为博主原创文 ...

  4. Java-Web中访问某个指定工程中的文件,报错后发现访问的文件是另一个工程里面的文件

    问题: 浏览器向我的bingou项目中的UserDaoImpl.java发送请求, myeclipse报错:空指针异常 点击报错行之后,错误给定位到了另一个项目中的的一个文件 解决: 原因是文件名错误 ...

  5. JVM插码之五:Java agent+ASM实战--监控所有方法执行时间

    本文建立在对instrumentation和agent有初步的了解的前提下阅读,关于这2个类的讲解在其它文章中. 这是一个maven项目,pom中需要的配置,lib中有asm的jar包 pom.xml ...

  6. 学习笔记之Redis

    Redis https://redis.io/ redis.cn http://www.redis.cn/ Azure Redis Cache Documentation - Tutorials, A ...

  7. 学习笔记之100 TOP Ikm C++ Online Test Questions

    100 TOP Ikm C++ Online Test Questions 2017 http://interviewquestionstutorials.com/tag/100-top-ikm-c- ...

  8. Win CE 5.0 增加电池电量显示

    摘自 http://blog.csdn.net/li0531/article/details/8818243 同时只在右上角显示一个 Label 控件,因此代码量精简了很多. [DllImport(& ...

  9. [UE4]用向量表示方向

    向量的概念 一.物理角度的向量 1)向量就是具有大小和长度的量 2)向量就是空间空的箭头 3)向量可以随意平移 举例:力,force:速度,velcity.这些都是具有大小和方向的量,都可以看成是向量 ...

  10. TCP阻塞模式开发

    在阻塞模式下,在IO操作完成前,执行的操作函数将一直等候而不会立刻返回,该函数所在的进程会阻塞在这里.相反,在非阻塞模式下,套接字函数会立即返回,而不管IO是否完成,该函数所在的线程将继续运行.阻塞模 ...