java - swapping lines between Session.save() . what changes happens in the output . Kindly see the comment in the code. i'm new in java hibernate
问题描述
What happens in the output if I swap lines is not understandable to me Following changes in the output happen.if i don't swap
//Hibernate: insert into Person (age, name) values (?, ?)
//Hibernate: insert into Address (city, house, person_personId) values
//(?, ?, ?)
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// one to one---test
//What if we swap line 28 and 29
session.save(person);
session.save(address);
session.getTransaction().commit();
session.close();
and here is the output when i swap:
//Hibernate: insert into Address (city, house, person_personId)
//values (?, ?, ?)
//Hibernate: insert into Person (age, name) values (?, ?)
//Hibernate: update Address set city=?, house=?, person_personId=?
//where addressId=?
解决方案
如果当前在实体中填写依赖关系,则没有区别。
session.save
此时不会进入数据库(例如,如果 id 是生成的序列,则可以调用序列,仅此而已)。它仅在持久性上下文中注册给定实体。
实际INSERT
发生在commit
.
推荐阅读
- collections - ASP.NET Core 5 MVC:将模型绑定复杂集合发布回控制器实例化 vm 但没有数据
- jmeter - JMeter - 添加客户端证书和密钥
- javascript - 如何在继续执行之前在单独的函数中等待 API 响应?
- python - 有没有办法避免在格式化字符串中使用双引号,这些字符串在 Python 中包含引号
- python - 带有 Flask 的 Sphinx 文档
- c++ - C++ 问题,其中分配给变量的值正在更改,即使它尚未修改
- node.js - CastError:模型“Product”的路径“_id”处的值“undefined”(类型字符串)转换为 ObjectId 失败
- ruby-on-rails - Rails 6 如何存储从第 3 方 API 获取的 access_token
- sql-server - 将多个 .csv 文件批量插入到每个文件的表中
- reactjs - Anchor href=#id 在 react-router-dom 链接中不起作用