Getting started 20 - Cascade persistence
In the Java program, the objects and objects will be referenced to each other through some relationships. If there is an object already a persistent object, it should also be persisted intuitively by the object it refer to to maintain the integrity of the object between objects. Sex, this is the basic concept of persistence by reachability by visibility. If the associated imagination between the object is a tree map, from a persistent object to the root of the tree, if the parent node is a persistent object, the child node referenced by the parent node should be automatically persisted, and on the other hand If there is no way to refer to it by any parent node, it should be deleted from the database. Hibernate has not fully implemented the above concepts, which allows users to determine automatic persistence, and when the object is specified (such as multi-object one, one-to-many, etc.), you can decide the temporary associated with the persistent object Does the object have automatic persistence and how to automatically persist. Take the previous "Multi-Multi-Entity Image" topic as an example, before setting the ROOM property in the User category, we have to save save (), on the relationship map of the object, basically we should The implementation is stored User, while the persistence of Room should be completed without our special designation. In order to achieve this, we can use cascade to specify automatic persistence, such as modify the user. HBM.XML is:
User.hbm.xml
COLUMN = "room_id" Class = "Onlyfun.caterpillar.Room" Cascade = "save-update" /> The preset on cascade is None, which is not automatic persistence, so we must perform Save () on each of the objects to persist, and we specify that cascade is Save-Update, which means when we store When you update the USER, you will automatically last. It should be noted here that when using Cascade automatic persistence, first check the ID attribute of the associated object, the ID attribute of the unopened object is determined by UNSAVED-VALUE, the preset is null, if you use Long When primitive type, you must specify the default value, so we must change it on the ID image of "multi-to-one entity image" Room.hbm.xml. Room.hbm.xml id> If you don't want to set up the unsaved-value information, you can change the long to long, which can meet the preset unsaved-value as NULL settings. About Unsaved-Value further introduction, you can refer to here: http: / / www.hibernate.org.cn/76.html After modifying the image file, we can use the following ways to store data: Hibernatetest.java Import onlyfun.caterpillar. *; import net.sf.hibernate. *; Import net.sf.hibs. *; Public class hibernatetest { Public static void main (string [] args) throws hibernateException { SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory (); Room Room1 = New Room (); ROOM1.SETADDRESS ("NTU-M8-419"); User USER1 = New user (); User1.setname ("bush"); User1.setroom (ROOM1); User USER2 = New user (); User2.setname ("CATERPILLAR"); User2.setroom (ROOM1); Session session = sessionFactory.openSession (); Transaction tx = session.begintransaction (); Session.save (user1); Session.save (user2); TX.comMit (); session.close (); SessionFactory.Close (); } } This time we don't have to store ROOM. Take the Cascade to save-update, which is associated with User, will be automatically persisted when it is stored or updated, and then we can even make objects to save: Transaction tx = session.begintransaction (); User User = (user) session.get (user.class, new long (1)); Room Room = New Room (); Room.Setaddress ("NTU-M5-105"); User.setroom (room); TX.comMit (); session.close (); In addition to Save-Update, Cascade can also use delete, all, all-delete- orphan, delete- orphan, each setting, suggesting you to view the reference manual 9.8Lifecyles and Object Graphs, which has a detailed description, Instructions for the completion of persistence by reachability, you can refer to Hibernate In Action 4.3.