Hibernate的一个问题object references an unsaved transient instance - save the transi5
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的更多相关文章
- 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 ...
- object references an unsaved transient instance save the transient instance before flushing
object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...
- object references an unsaved transient instance - save the transient instance before flushing异常问题处理
一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...
- 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 ...
- 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(参考资料: ...
- hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing
我的问题出在,删除的对象对应的表中有一个外键,关联着另外一个表,可是另外一个表中没有数据,所以报了这个错误. 参考http://www.cnblogs.com/onlywujun/archive/20 ...
- object references an unsaved transient instance【异常】
[异常提示] TransientObjectException: 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顺序问题---比方学生表和 ...
- ManyToMany【项目随笔】关于异常object references an unsaved transient instance
在保存ManyToMany 时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.Tran ...
随机推荐
- vs2010开发环境恢复--(mysql,数据文件可直接拷贝,并可用navicat直接管理)
一.linq to mysql (DBLINQ) 1.安装mysql phpstudy2014,数据库文件可直接拷贝,在命令行中运行select version();查看版本为5.5.38 ,单独安装 ...
- java Scanner中的hasNext()方法
hasNext()方法判断输入(文件.字符串.键盘等输入流)是否还有下一个输入项,若有,返回true,反之false. Scanner sc = new Scanner(new File(" ...
- 【Reporting Services 报表开发】— 怎么根据当前表单的guid作为参数查询相关数据?
select AId from FilteredA as CRMAF_FilteredA 用这个 作为一个DataSet1 , 然后添加在报表里面添加一个参数 @AId,设置的默认的查询为前面Data ...
- 使用scrapy框架爬取自己的博文(3)
既然如此,何不再抓一抓网页的文字内容呢? 谷歌浏览器有个审查元素的功能,就是按树的结构查看html的组织形式,如图: 这样已经比较明显了,博客的正文内容主要在div 的class = cnblogs_ ...
- Java-Runoob-高级教程-实例-数组:06. Java 实例 – 数组获取最大和最小值
ylbtech-Java-Runoob-高级教程-实例-数组:06. Java 实例 – 数组获取最大和最小值 1.返回顶部 1. Java 实例 - 数组获取最大和最小值 Java 实例 以下实例 ...
- Mysql-binlog的移动和归档
#!/bin/bash # To backup and archive binlogs. declare -i NUM=0 declare -i SUM=0 SUM=`/bin/ls -l mysql ...
- linux修改文件系统挂载的目录
比如想把已经挂载在home目录上的硬盘挂载到data目录上, 如下操作 #df -h(查看分区情况及数据盘名称) # mkdir /data(如果没有data目录就创建,否则此步跳过) # umoun ...
- ZooKeeper 集群的安装、配置---Dubbo 注册中心
ZooKeeper 集群的安装.配置.高可用测试 Dubbo 注册中心集群 Zookeeper-3.4.6 Dubbo 建议使用 Zookeeper 作为服务的注册中心. Zookeeper 集群中只 ...
- MySQL-Jira双机热备
主服务器:192.168.1.23 从服务器:192.168.1.243 一.主服务器Master配置 1. 创建同步账号.赋权 在主服务器上为从服务器建立一个连接帐户,此处用root,该帐户必须授予 ...
- ThinkPHP 5使用 Composer 组件名称可以从https://packagist.org/ 搜索到
http://www.phpcomposer.com/ 1 这个是国内的composer网站 thinkphp5自带了composer.phar组件,如果没有安装,则需要进行安装 以下命令全部在项目目 ...