cascade and inverse of hibernate in java

Keywords: PHP Session Java Struts Apache

1.Cascade is a cascade action. In many_to_one, if cascade is used to cascade related objects, the following code can cascade to save Category objects.

Mapping File Settings in Book

<many-to-one name="category" column="cid" cascade="save-update"/>

When saving a book, if the corresponding category of the book is not saved, save the category first, then save the book, and complete the cascade save data action.

The default value of Cascade is none, no cascade action is performed;

Cascade can be delete, and cascade will not be set to delete in many_to_one because exceptions may be created unless they are one-to-one based on foreign keys.

Cascade can also be all, all means that all cascade actions can be performed.

 

Use cascade in one_to_many:

<!--Two-way one-to-many settings-->
        <set name="books" cascade="save-update">
            <!--Set Foreign Key-->
            <key column="cid"></key>
            <!--Type of the side with more settings-->
            <one-to-many class="Book"/>
        </set>

When one end of the data is saved, the book is saved cascaded if it is found that the relational object multi-end data is not saved; however, when one end of the data is saved cascaded, there are n more update statements, which is inefficient.(that is, when cascading saves at one end, there will be n more update statements, which is inefficient; therefore, cascading is recommended at multiple ends rather than at one end)

 

If one end of the cascade="delete", then when one end of the data is deleted, all data from the multiple ends will be deleted in a cascade.(Use with caution)

Note: To make cascade work, you must set up an associated object.Cascade does not work if the associated object does not exist.It is recommended that cascade be used sparingly or even not.

2. inverse: Reverse, used in hibernate to control who manages the relationship (foreign key) at one end.

<!--Two-way one-to-many settings-->
        <set name="books" cascade="save-update" inverse="true">
            <!--Set Foreign Key-->
            <key column="cid"></key>
            <!--Type of the side with more settings-->
            <one-to-many class="Book"/>
        </set>

Represents that the relationship (foreign key) is maintained by one end of the Book (that is, one end of the many).That is, the foreign key is maintained (saved) only if the Category property corresponding to the book object is set for the book object.

  @Test
    public void testInit(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Category c1 = new Category("Computer Class");
            Category c2 = new Category("literature");
            Category c3 = new Category("History");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Book b1 = new Book("java","sun",30,df.parse("1995-05-23"));
            b1.setCategory(c1);
            Book b2 = new Book("struts","apache",40,df.parse("2006-09-12"));
            b2.setCategory(c1);
            Book b3 = new Book("What happened in the Ming Dynasty","The bright moon of that year",70,df.parse("2008-05-23"));
            b3.setCategory(c3);
            Book b4 = new Book("Water Margin","Old tear",20,df.parse("1985-05-23"));
            b4.setCategory(c2);
            c1.getBooks().add(b1);
            c1.getBooks().add(b2);
            c2.getBooks().add(b4);
            c3.getBooks().add(b3);
            session.save(c1);
            session.save(c2);
            session.save(c3);
            tx.commit();
            
        } catch (Exception e) {
            if(tx!=null)
                tx.rollback();
        }finally {
            HibernateUtil.close();
        }
    }

If inverse=false, the relationship can also be maintained at one end, that is, the foreign key relationship can be saved by adding more data at one end.

  @Test
    public void testInit(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Category c1 = new Category("Computer Class");
            Category c2 = new Category("literature");
            Category c3 = new Category("History");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Book b1 = new Book("java","sun",30,df.parse("1995-05-23"));
            Book b2 = new Book("struts","apache",40,df.parse("2006-09-12"));
            Book b3 = new Book("What happened in the Ming Dynasty","The bright moon of that year",70,df.parse("2008-05-23"));
            Book b4 = new Book("Water Margin","Old tear",20,df.parse("1985-05-23"));
            c1.getBooks().add(b1);
            c1.getBooks().add(b2);
            c2.getBooks().add(b4);
            c3.getBooks().add(b3);
            session.save(c1);
            session.save(c2);
            session.save(c3);
            tx.commit();
            
        } catch (Exception e) {
            if(tx!=null)
                tx.rollback();
        }finally {
            HibernateUtil.close();
        }
    }

This is done by updating the statement.So in general, inverse=true.

There is no inverse on one end of the many because the many end maintains relationships by default.

Note: Cascading and inverse are done by associating objects, neither of which works if no associating objects are set.When both are set up, care should be taken to distinguish who manages cascades and who manages relationships.It is possible that an associated object manages both relationships and cascades.

Posted by zeberdeee on Thu, 18 Jul 2019 09:43:44 -0700