【问题标题】:why after Hibernate4 save() the database will change?为什么在 Hibernate4 save() 之后数据库会改变?
【发布时间】:2013-12-27 11:18:30
【问题描述】:

为什么在没有flush()的情况下执行这样的代码后,数据库会改变?

public static void main(String args[]){

ApplicationContext ac = new FileSystemXmlApplicationContext("src/main/resources/config/spring/spring-common.xml");

SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");

Session session = sessionFactory.openSession();

Student student = new Student();

student.setName("zyc");

session.save(student);
}

我使用这个类来创建 sessionFactory。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

【问题讨论】:

    标签: hibernate-4.x


    【解决方案1】:

    如果将 Student 定义为使用 IDENTITY 生成 ID,则此处的“数据库将发生变化”。这意味着 Student 的插入将在调用 save 用于基于 IDENTITY 的生成器时立即发生。

    【讨论】:

    • 哇,没想到是id生成引起的,非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2020-12-19
    相关资源
    最近更新 更多