The meaning of cascade in Hibernate/JPA

In a Java program, objects and objects refer to each other through certain relationships. If an object is already a persistent object, the object referenced by it should intuitively also be persistent to maintain the integrity of the association between objects This is the basic concept of Persistence by reachability.

If you imagine the association between objects as a tree diagram, starting from a persistent object as the root of the tree, if the parent node is a persistent object, the child nodes referenced by the parent node should be automatically persisted. If there is a child node that cannot be referenced by any parent node, then there is no requirement for it to be persisted, and it should be deleted from the database.

Whether all objects in the complete scan object diagram should be persisted involves the scanning of all related tables in the memory and the database. This concept cannot be accepted in terms of performance, so Hibernate does not fully implement the above concept, it lets use You decide the way of automatic persistence. When objects are specified to be associated (for example, many-to-one, one-to-many, etc.), you can decide whether the temporary objects associated with the persistent objects are automatically persisted and how .

In Hibernate mapping files cascade property is on the label is set, the default is none to many to one example of, if not cascade set to true, you must separately User instance instance Room Save:

Room room1 = new Room ();
room1.setAddress ("NTU-M8-419");
Room room2 = new Room ();
room2.setAddress ("NTU-G3-302");
User user1 = new User ();
user1.setName ("bush");
user1.setRoom (room1);
User user2 = new User ();
user2.setName ("caterpillar");
user2.setRoom (room1);
User user3 = new User ();
user3. setName ("momor");
user3.setRoom (room2);
Session session = HibernateUtil.getSessionFactory().openSession ();
Transaction tx = session.beginTransaction ();
     

// store Room instance
session.save (room1);
session.save (room2);       
// store User instance
session.save (user1);
session.save (user2);
session.save (user3); tx.commit (); session.close ();
     

When the cascade attribute needs to set multiple values, you can use comma to separate, for example:
        <many-to-one name = "room"
                     column = "room_id"
                     class = "onlyfun.caterpillar.Room"
                     cascade = "save-update, pesist , merge "
                     outer-join =" true "/>

When using cascade for automatic persistence, the id attribute of the associated object will be checked first. Whether the unpersisted object is stored is determined by the unsaved-value of the id attribute, the default It is null, that is, the object is stored when there is no reference to any value. If you use a primitive type such as int or long, the initial value of the data member will be set to 0, so you must specify the default value yourself , For example (if the data type of id is long):
....
        <id name = "id" column = "id" unsaved-value = "0">
            <generator class = "native" />
        </ id >
....

cascade Type

CascadeType.ALL means it will do all actions.

CascadeType.PERSIST: When persisting an entity, also persist the entities held in this field. We suggest liberal application of this cascade rule, because if the EntityManager finds a field that references a new entity during flush, and the field does not use CascadeType.PERSIST, it is an error.

CascadeType.REMOVE: When deleting an entity, also delete the entities held in this field.

CascadeType.REFRESH: When refreshing an entity, also refresh the entities held in this field.

CascadeType.MERGE: When merging entity state, also merge the entities held in this field.


Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)