hibernate sets inverse, cascade, fetch

  * fetch={select/join}, the default is select.

  * fetch="select" means to send another sql statement for associated query.

  * fetch="join" means to perform table join query. When set to join, lazy=true has no effect.

 

  * The update of the relationship controlled by inverse is performed during session.flush, so the update of the relationship is triggered during save/update

    insert/update/delete sql is always executed last. The update/delete sql of the update association caused by delete is always in

    Execute first.

  * inverse is only valid for one2many many2many, not valid for many2one one2one.

    Since the one side will issue an update update sql when maintaining the association relationship, the association relationship is usually maintained on the many side.

 

  * cascade generally does not set cascade deletion for many2one, many2many, and one2one with constrained=true.
  * Several values ​​of cascade:
     save-update: cascade save (if the sub-object is updated after load, it will also cascade update). But it will not cascade delete
     delete: cascade delete, but does not have cascade save and update
     all-delete-orphan: When the parent-child relationship is released, automatically delete the child objects that do not belong to the parent object, and also support cascade delete and cascade save update.
     all: cascade delete, cascade update, but when the parent-child relationship is released Child objects are not automatically deleted. 
     delete-orphan: delete all objects that are disassociated from the current object

 

  * When setting cascade="all-delete-orphan" in the set of one2many and many2many, in update/flush/commit

    When you can't directly set clazz.setStudents(null);

    会引发异常:A collection with cascade="all-delete-orphan" was no longer referenced by the owning

    entity instance. During the delete operation, you can directly set null, which will only delete the clazz table and update the Student table records.

    When creating a new one, you can set clazz.setStudents(null); this will only save the clazz table records.

 


  * one2many one-way association:
    <set name="students" cascade="save-update">
         <key>
             <column name="clazz_id" length="32" />
        </key>
        <one-to-many class= "com.jaeson.hibernatestudy.bean.Student" />
    </set>
    If the set is set to inverse="true", the association relationship will be maintained by multi-party students. Because it is a one-way association, multi-party students will not be able to

    A foreign key is maintained, i.e. clazz_id will be null.
    The default inverse="false" means that the foreign key relationship is maintained by one side Clazz, so the update statement will be issued for updating when saving

    Student's foreign key clazz_id.

    When executing session.save(student), if clazz_id is not-null, an item insertion exception will occur, so one2many one-way

    The attribute of the foreign key clazz_id needs to be set to not-null="false" during association.
    cascade="save-update" means to cascade the associated Student objects in Set when saving Clazz, usually used for collections such as <Set>

    association. If the cascade attribute is not set, the Student object session.save(student) must be saved explicitly, otherwise Clazz is maintaining

    An org.hibernate.TransientObjectException will be thrown when an association is involved.

    Hibernate: insert into db4myeclipse.clazz (name, id) values (?, ?)
    Hibernate: insert into db4myeclipse.student (name, sex, id) values (?, ?, ?)
    Hibernate: update db4myeclipse.student set clazz_id=? where id=?

  * many2one 单向关联:

    You need to save one Clazz first, and then save multiple Students, so there will only be 2 insert sqls. Otherwise, 2 inserts and a

    Update the sql of the association relationship.

    <many-to-one name="clazz" class="com.jaeson.hibernatestudy.bean.Clazz" fetch="join">
        <column name="clazz_id" length="32" not-null="true" / >
    </many-to-one>
    When fetch="join", lazy="true" has no effect, and inner join query will be performed together with Student.
    Since the foreign key association is maintained by multiple students, the foreign key clazz_id can be set to not-null="true".
    When saving, you need to save the associated object Clazz first:

    session.save(clazz);
    session.save(student);

    Otherwise Student will throw when maintaining the association:

    org.hibernate.action.internal.UnresolvedEntityInsertActions异常。
    Attempting to save one or more entities that have a non-nullable association with an unsaved

    transient entity.
    Hibernate: insert into db4myeclipse.clazz (name, id) values (?, ?)
    Hibernate: insert into db4myeclipse.student (clazz_id, name, sex, id) values (?, ?, ?, ?)

 

 

  * many2one bidirectional association:












    org.hibernate.action.internal.UnresolvedEntityInsertActions异常。
    Attempting to save one or more entities that have a non-nullable association with an unsaved

    transient entity.
    If Clazz sets cascade="save-update", you can just execute session.save(clazz).
    Hibernate: insert into db4myeclipse.clazz (name, id) values ​​(?, ?)
    Hibernate: insert into db4myeclipse.student (clazz_id, name, sex, id) values ​​(?, ?, ?, ?)

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041673&siteId=291194637