首页 > 解决方案 > 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=?

标签: javahibernatejpa

解决方案


如果当前在实体中填写依赖关系,则没有区别。

session.save此时不会进入数据库(例如,如果 id 是生成的序列,则可以调用序列,仅此而已)。它仅在持久性上下文中注册给定实体。

实际INSERT发生在commit.


推荐阅读