The cascade of java hibernate and inverse

1.Cascade cascade operation, if a cascade operation may be cascaded in many_to_one associated object, the following code can be cascaded saved Category object.

Set in the Book of mapping file

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

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

Cascade default is none, not cascade operation;

Cascade can delete, in cascade many_to_one is not set to delete, because it would create an exception, unless one is based on foreign keys.

Cascade also think that all, all indicating that you can cascade all the action.

 

In one_to_many in cascade:

<! - bidirectional many settings -> 
        <SET name = " Books " Cascade = "Save-Update"> 
            <! - foreign key -> 
            <Key column = "CID"> </ Key> 
            <! - set the plurality of types of end -> 
            <One-to-mANY class = "Book" /> 
        </ sET>

In the end when saving data, if the relationship between objects found multiport data is not saved, it will cascade to save book; however cascading save at one end, will be more of the n update statement, the efficiency is relatively low. (I.e., when the end use of the cascade has been saved, a plurality of n update statement inefficient; cascade multiport therefore recommended, not recommended for use in one end)

 

If cascade = "delete" at one end, then at the end when you delete data, all data will cascade multiport deleted. ( Caution )

Note: cascade to work, be sure to set the associated object. If the associated object does not exist, then there will be no cascade effect. Recommended to use less cascade, you do not even have.

2. inverse: reverse for control relationship to an end of a (foreign key) in the hibernate who management (CRUD) .

<! - bidirectional many settings -> 
        <SET name = "Books" Cascade = "Save-Update" inverse = "to true"> 
            <! - foreign key -> 
            <Key column = "CID" > </ Key> 
            <-! type disposed at one end of the multi -> 
            <One-to-mANY class = "Book" /> 
        </ sET>

It shows a relationship (foreign key) to maintain an end of the Book (i.e., multi-end) . That is a good book to set the corresponding object attribute to book Category objects, foreign keys will be maintained (stored).

  @Test
    public void testInit(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Category c1 = new Category("计算机类");
            Category c2 = new Category("文学");
            Category c3 = new Category("历史");
            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("明朝那些事儿","当年明月",70,df.parse("2008-05-23"));
            b3.setCategory(c3);
            Book b4 = new Book("水浒传","老撕",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 the inverse = false, may be described a maintenance end relationship , that is to say by adding more than one end of the data at one end, to hold foreign key relationships.

  @Test
    public void testInit(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Category c1 = new Category("计算机类");
            Category c2 = new Category("文学");
            Category c3 = new Category("历史");
            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("明朝那些事儿","当年明月",70,df.parse("2008-05-23"));
            Book b4 = new Book("水浒传","老撕",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();
        }
    }

However, to be completed by the update statement. Therefore, under normal circumstances, inverse = true.

In the end not much inverse, because many end default can maintain the relationship .

Note: cascade and inverse association objects are accomplished by, if no associated objects, both of which do not work. In the case of both settings, to distinguish between who managed cascade, who manage the relationship. There may be an associated target both management and relationship management cascade.

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11210864.html