hibernate级联情况下以commit或者flush来进行级联提交

来源:互联网 发布:软件人力外派 编辑:程序博客网 时间:2024/05/17 04:51


1.2.3. 使关联工作

Now we will bring some people and events together in a new method in EventManager:

    private void addPersonToEvent(Long personId, Long eventId) {        Session session = HibernateUtil.getSessionFactory().getCurrentSession();        session.beginTransaction();        Person aPerson = (Person) session.load(Person.class, personId);        Event anEvent = (Event) session.load(Event.class, eventId);        aPerson.getEvents().add(anEvent);        session.getTransaction().commit();    }

After loading a Person and an Event, simply modify the collection using the normal collection methods. There is no explicit call to update() or save(); Hibernate automatically detects that the collection has been modified and needs to be updated. This is called automatic dirty checking. You can also try it by modifying the name or the date property of any of your objects. As long as they are in persistent state, that is, bound to a particular Hibernate org.hibernate.Session, Hibernate monitors any changes and executes SQL in a write-behind fashion. The process of synchronizing the memory state with the database, usually only at the end of a unit of work, is called flushing. In our code, the unit of work ends with a commit, or rollback, of the database transaction.


0 0
原创粉丝点击