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. ionic3之自定义tabs菜单图标

    请参考以下链接 ionic3之自定义tabs菜单图标

  2. C#设计模式之:抽象工厂模式与反射

    抽象工厂模式[实例]:定义一个用于创建对象的接口,让子类决定实例化哪一个类 UML 代码class User{    private int _id;    public int Id { get = ...

  3. ASP.NET 实现验证码以及刷新验证码

    实现代码 /// <summary> /// 生成验证码图片,保存session名称VerificationCode /// </summary> public static ...

  4. Apache2.4.7 + php5 + mysql thinkphp

    1. LAMP 的安装sudo apt-get install apache2 2.安装PHP sudo apt-get install  libapache2-mod-php5 php5 php5- ...

  5. elasticsearch 复杂查询小记

    以下接口调用都基于5.5版本 JSON 文档格式 { "_index": "zipkin-2017-09-06", "_type": &qu ...

  6. OSPF里几个特殊区域(stub、Totally stubby、NSSA、Totally NSSA)总结

    网友总结: 简单的说,就是 stub过滤4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外部路由 total stub过滤3,4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外 ...

  7. sklearn的BaseEstimator、transformerMixin、ClassifierMixin、RegressorMixin、ClusterMixin介绍

    class sklearn.base.BaseEstimator:为所有的estimators提供基类 方法: __init__() 初始化方法 get_params(deep=True) 获取这个估 ...

  8. 开发组件:REST API

    REST API 最佳入门指南 https://blog.csdn.net/px01ih8/article/details/78674685

  9. javascript变量作用域 — 全局变量

    javascript中,如果没有用var 声明一个变量,则该变量会被自动创建在全局作用域中,即使你是在某个函数中创建的,它也会成为全局变量,从而可以被另一个函数调用.

  10. Python面向对象之内置方法

    1.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 issubclass(sub, s ...