hibernate configuration of the inverse = true explanation

 

 

 

First inverse = "true" is used in bidirectional way associated with this configuration there is no correlation

inverse - marked by which side to maintain the relationship (will use two-way association)

inverse default value is false

If the inverse is set to true, that will maintain the relationship between the two other

for example

OR mapping file

contactPerson mapping file

<class name="cn.com.leadfar.hibernate.ContactPerson" table="t_person" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name" />

      <property name="address"/>

      <property name="qq" />

      <! - This many-to-end and a gid to an end if not consistent with maintaining a consistent reference point group ->

      <many-to-one name="group" column="gid1"></many-to-one>

   </class>

Group related configuration files

<class name="cn.com.leadfar.hibernate.Group" table="t_group" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name"/>

      <! - inverse = true is not provided at either end of the management can be represented by ->

      <! - set inverse = true identity of a forced end to manage their relationship is inverse = default false case of both parties can manage relationships ->

      <set name="persons" lazy="extra"   >

          <! - end gid keep more consistent ->

          <key column="gid2"></key>

          <one-to-many class="cn.com.leadfar.hibernate.ContactPerson"/>

      </set>

   </class>

 

 <set name="students“ lazy=“false” inverse=“true”>

        <key column="classesid" ></key>

        <one-to-many class=“cn.com.leadfar.Student" />

 </set>

Mandatory provisions linked to relations with the other two sides

Inverse = "false" represents both parties can manage this relationship (default)

Profiles

When Example Inverse = "false"

Code:

session.beginTransaction();
ContactPerson cp1 = new ContactPerson("炉小鱼");
session.save(cp1);
ContactPerson cp2 = new ContactPerson("比尔盖茨");
session.save(cp2);
ContactPerson cp3 = new ContactPerson("巴菲特");
session.save(cp3);

Group g1 =new Group("组0");
session.save(g1);
Group g2 = new Group("组1");
session.save(g2);
Group g3 = new Group("组3");
session.save(g3);
Set<ContactPerson> persons1 = new HashSet<ContactPerson>();
persons1.add(cp1);
persons1.add(cp2);
g1.setPersons(persons1);
Set<ContactPerson> persons2 = new HashSet<ContactPerson>();
persons2.add(cp3);
g3.setPersons (persons2);
session.getTransaction().commit();

Export

All gid1 maintenance is null by the end of a

 

// ************** two-way manage their relationship from one end and a number of end ******************* ********* 
session.beginTransaction ();
the ContactPerson the ContactPerson new new CP1 = ( "fish furnace");
Session.save (CP1);
the ContactPerson the ContactPerson CP2 new new = ( "Bill Gates");
the session. Save (CP2);
the ContactPerson the ContactPerson new new CP3 = ( "Buffet");
Session.save (CP3);

group G1 = new new group ( "group 0");
g1.addPerson (CP1);
g1.addPerson (CP2);
the session .save (g1); // g1 becomes a persistent object state, now has a database to identify
// but also to establish
cp1.setGroup (g1);
cp2.setGroup (g1);
group g2 = new new group ( "group 1 ");
Session.save (G2); //// G2 into a persistent state of the object now has a database identifying

group g3 = new group (" group. 3 ");
g3.addPerson (CP3);
Session.save (g3);//// g3 into a persistent state of the object, now has a database ID
cp3.setGroup(g3);
session.getTransaction().commit();

Export

From one end to manage their relationship

// ***************** from one end of a relationship to associate their ********************* ****** 
group G1 = new new group ( "group 0");
Session.save (G1);
group G2 = new new group ( "group. 1");
Session.save (G2);
group group new new G3 = ( "group. 3");
Session.save (G3);

the ContactPerson the ContactPerson new new CP1 = ( "furnace rain");
cp1.setGroup (G1); // association relationship
Session.save (CP1);

the ContactPerson the ContactPerson CP2 new new = ( "Bill Gates");
cp2.setGroup (g1); // build relationships
were saved using Session.save (CP2);

ContactPerson CP3 = new new ContactPerson ( "Buffett");
cp3.setGroup (G3); // associate relations
session .save (cp3);

Export

These are the end and in the end more than a case of inverse = "false" can manage their relationship

To follow the end of a set of inverse = true

<class name="cn.com.leadfar.hibernate.Group" table="t_group" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name"/>

      <! - inverse = true is not provided at either end of the management can be represented by ->

      <set name="persons" lazy="extra"  inverse="true" >

          <! - end gid keep more consistent ->

          <key column="gid2"></key>

          <one-to-many class="cn.com.leadfar.hibernate.ContactPerson"/>

      </set>

   </class>

Management continues to run from one end of the test code runs much

we analyze this code
g1.addPerson (cp1); // If you set inverse = true at one end, then this code will not use the
g1.addPerson (cp2) ; // if provided at one end of a inverse = true, then would not use this code, so as to null the field gid 
cp1.setGroup (g1); // will work more end management
cp2.setGroup ( g1); // multi-end management will work
only associated with this approach is gid1 will have a value 
which is inverse = -true gives us an intuitive result, set inverse = true he will not be the end of a to manage the relationship between the one and the many

Inverse = true role

Field to just two in order to see more clearly set inverse = true to indicate which field has a value which field values ​​will not see more clearly if you set inverset = true imperatives in which end their relationship management generally set consistent field

<class name = "cn.com.leadfar.hibernate.ContactPerson" Table = "t_person">
<name ID = "ID">
<Generator class = "Native" />
</ ID>
<Property name = "name" / >
<Property name = "address" />
<Property name = "QQ" />
<-! this many-to-end and a gid to an end if not consistent with maintaining a consistent reference point group ->
< One-to-name MANY = "Group" column = "GID"> </ MANY-to-One>
</ class>

 

<class name = "cn.com.leadfar.hibernate.Group" Table = "t_group">
<name ID = "ID">
<Generator class = "Native" />
</ ID>
<Property name = "name" / >
<! - do not set inverse = true representation can be managed either end ->
<! - set inverse = true identity of a forced end to manage their relationship ->
<the sET name = "persons" lazy = " Extra "= inverse" to true ">
<-! gid keep more consistent end ->
<Key column =" gid "> </ Key>
<One-to-mANY class =" cn.com.leadfar. hibernate.ContactPerson "/>
</ SET>
</ class>

// not set inverse = "true" then the end of the update data of a
Group G = (Group) Session.load (Group.class,. 1);
System.out.println (g.getName ());
G. setName ( "friends");
session.getTransaction () the commit ();.
in multi-tier application architecture inside if you want to update an object first is to check out according to id, id will update interface objects are displayed inside the final submission to update the action inside, saying that this means that there is usually not the case submitted to update the background to update the new model to update the action which gives relevant id values and related properties in this way to update more common

CP1 = new new the ContactPerson the ContactPerson ();
cp1.setId (. 1);
cp1.setName ( "HelloWorld");
cp1.setAddress ( "plain");
cp1.setQq ( "3333222"); 
// time to be updated gid null
Session.update (CP1);
session.getTransaction () the commit ();.
output:

session.beginTransaction ();
the ContactPerson the ContactPerson new new CP1 = ();
cp1.setId (. 1);
cp1.setName ( "HelloWorld");
cp1.setAddress ( "plain");
cp1.setQq ( "3,333,222"); 
// gid will then update is null
// we have to remember his association
Group g1 = new new Group ();
g1.setId (1);
cp1.setGroup (g1);

Session.update (cp1);

Export

session.beginTransaction();
Group g1 = new Group();
g1.setId(1);
g1.setName("朋友");
session.update(g1);



I do not want this to happen, we have found associated with lost if the association does not want to lose how to do it?
The simplest way is to set inverse = true
set to true inverse = Results
session.beginTransaction ();
Group Group G1 = new new ();
g1.setId (. 1);
g1.setName ( "SB34 Friends");

the ContactPerson CP1 = the ContactPerson new new ();
cp1.setId (. 1);
the ContactPerson the ContactPerson CP2 new new = ();
cp2.setId (2);

g1.addPerson (CP1);
g1.addPerson (CP2);
Session.update (G1);

the session. getTransaction () commit ();.
find a collection of saved things inside us is whether he, so inverse = true no matter in this set there are no data
public class Group {
Private int the above mentioned id;
Private String name;
Private the Set <ContactPerson> persons = new HashSet <ContactPerson> () ;
I will not go inside the synchronized set of data when stored and updated on the inside when the set of data does not exist or does not matter and I do not care

First inverse = "true" is used in bidirectional way associated with this configuration there is no correlation

inverse - marked by which side to maintain the relationship (will use two-way association)

inverse default value is false

If the inverse is set to true, that will maintain the relationship between the two other

for example

OR mapping file

contactPerson mapping file

<class name="cn.com.leadfar.hibernate.ContactPerson" table="t_person" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name" />

      <property name="address"/>

      <property name="qq" />

      <! - This many-to-end and a gid to an end if not consistent with maintaining a consistent reference point group ->

      <many-to-one name="group" column="gid1"></many-to-one>

   </class>

Group related configuration files

<class name="cn.com.leadfar.hibernate.Group" table="t_group" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name"/>

      <! - inverse = true is not provided at either end of the management can be represented by ->

      <! - set inverse = true identity of a forced end to manage their relationship is inverse = default false case of both parties can manage relationships ->

      <set name="persons" lazy="extra"   >

          <! - end gid keep more consistent ->

          <key column="gid2"></key>

          <one-to-many class="cn.com.leadfar.hibernate.ContactPerson"/>

      </set>

   </class>

 

 <set name="students“ lazy=“false” inverse=“true”>

        <key column="classesid" ></key>

        <one-to-many class=“cn.com.leadfar.Student" />

 </set>

Mandatory provisions linked to relations with the other two sides

Inverse = "false" represents both parties can manage this relationship (default)

Profiles

When Example Inverse = "false"

Code:

session.beginTransaction();
ContactPerson cp1 = new ContactPerson("炉小鱼");
session.save(cp1);
ContactPerson cp2 = new ContactPerson("比尔盖茨");
session.save(cp2);
ContactPerson cp3 = new ContactPerson("巴菲特");
session.save(cp3);

Group g1 =new Group("组0");
session.save(g1);
Group g2 = new Group("组1");
session.save(g2);
Group g3 = new Group("组3");
session.save(g3);
Set<ContactPerson> persons1 = new HashSet<ContactPerson>();
persons1.add(cp1);
persons1.add(cp2);
g1.setPersons(persons1);
Set<ContactPerson> persons2 = new HashSet<ContactPerson>();
persons2.add(cp3);
g3.setPersons (persons2);
session.getTransaction().commit();

Export

All gid1 maintenance is null by the end of a

 

// ************** two-way manage their relationship from one end and a number of end ******************* ********* 
session.beginTransaction ();
the ContactPerson the ContactPerson new new CP1 = ( "fish furnace");
Session.save (CP1);
the ContactPerson the ContactPerson CP2 new new = ( "Bill Gates");
the session. Save (CP2);
the ContactPerson the ContactPerson new new CP3 = ( "Buffet");
Session.save (CP3);

group G1 = new new group ( "group 0");
g1.addPerson (CP1);
g1.addPerson (CP2);
the session .save (g1); // g1 becomes a persistent object state, now has a database to identify
// but also to establish
cp1.setGroup (g1);
cp2.setGroup (g1);
group g2 = new new group ( "group 1 ");
Session.save (G2); //// G2 into a persistent state of the object now has a database identifying

group g3 = new group (" group. 3 ");
g3.addPerson (CP3);
Session.save (g3);//// g3 into a persistent state of the object, now has a database ID
cp3.setGroup(g3);
session.getTransaction().commit();

Export

From one end to manage their relationship

// ***************** from one end of a relationship to associate their ********************* ****** 
group G1 = new new group ( "group 0");
Session.save (G1);
group G2 = new new group ( "group. 1");
Session.save (G2);
group group new new G3 = ( "group. 3");
Session.save (G3);

the ContactPerson the ContactPerson new new CP1 = ( "furnace rain");
cp1.setGroup (G1); // association relationship
Session.save (CP1);

the ContactPerson the ContactPerson CP2 new new = ( "Bill Gates");
cp2.setGroup (g1); // build relationships
were saved using Session.save (CP2);

ContactPerson CP3 = new new ContactPerson ( "Buffett");
cp3.setGroup (G3); // associate relations
session .save (cp3);

Export

These are the end and in the end more than a case of inverse = "false" can manage their relationship

To follow the end of a set of inverse = true

<class name="cn.com.leadfar.hibernate.Group" table="t_group" >

      <id name="id">

          <generator class="native"/>

      </id>

      <property name="name"/>

      <! - inverse = true is not provided at either end of the management can be represented by ->

      <set name="persons" lazy="extra"  inverse="true" >

          <! - end gid keep more consistent ->

          <key column="gid2"></key>

          <one-to-many class="cn.com.leadfar.hibernate.ContactPerson"/>

      </set>

   </class>

Management continues to run from one end of the test code runs much

we analyze this code
g1.addPerson (cp1); // If you set inverse = true at one end, then this code will not use the
g1.addPerson (cp2) ; // if provided at one end of a inverse = true, then would not use this code, so as to null the field gid 
cp1.setGroup (g1); // will work more end management
cp2.setGroup ( g1); // multi-end management will work
only associated with this approach is gid1 will have a value 
which is inverse = -true gives us an intuitive result, set inverse = true he will not be the end of a to manage the relationship between the one and the many

Inverse = true role

Field to just two in order to see more clearly set inverse = true to indicate which field has a value which field values ​​will not see more clearly if you set inverset = true imperatives in which end their relationship management generally set consistent field

<class name = "cn.com.leadfar.hibernate.ContactPerson" Table = "t_person">
<name ID = "ID">
<Generator class = "Native" />
</ ID>
<Property name = "name" / >
<Property name = "address" />
<Property name = "QQ" />
<-! this many-to-end and a gid to an end if not consistent with maintaining a consistent reference point group ->
< One-to-name MANY = "Group" column = "GID"> </ MANY-to-One>
</ class>

 

<class name = "cn.com.leadfar.hibernate.Group" Table = "t_group">
<name ID = "ID">
<Generator class = "Native" />
</ ID>
<Property name = "name" / >
<! - do not set inverse = true representation can be managed either end ->
<! - set inverse = true identity of a forced end to manage their relationship ->
<the sET name = "persons" lazy = " Extra "= inverse" to true ">
<-! gid keep more consistent end ->
<Key column =" gid "> </ Key>
<One-to-mANY class =" cn.com.leadfar. hibernate.ContactPerson "/>
</ SET>
</ class>

// not set inverse = "true" then the end of the update data of a
Group G = (Group) Session.load (Group.class,. 1);
System.out.println (g.getName ());
G. setName ( "friends");
session.getTransaction () the commit ();.
in multi-tier application architecture inside if you want to update an object first is to check out according to id, id will update interface objects are displayed inside the final submission to update the action inside, saying that this means that there is usually not the case submitted to update the background to update the new model to update the action which gives relevant id values and related properties in this way to update more common

CP1 = new new the ContactPerson the ContactPerson ();
cp1.setId (. 1);
cp1.setName ( "HelloWorld");
cp1.setAddress ( "plain");
cp1.setQq ( "3333222"); 
// time to be updated gid null
Session.update (CP1);
session.getTransaction () the commit ();.
output:

session.beginTransaction ();
the ContactPerson the ContactPerson new new CP1 = ();
cp1.setId (. 1);
cp1.setName ( "HelloWorld");
cp1.setAddress ( "plain");
cp1.setQq ( "3,333,222"); 
// gid will then update is null
// we have to remember his association
Group g1 = new new Group ();
g1.setId (1);
cp1.setGroup (g1);

Session.update (cp1);

Export

session.beginTransaction();
Group g1 = new Group();
g1.setId(1);
g1.setName("朋友");
session.update(g1);



I do not want this to happen, we have found associated with lost if the association does not want to lose how to do it?
The simplest way is to set inverse = true
set to true inverse = Results
session.beginTransaction ();
Group Group G1 = new new ();
g1.setId (. 1);
g1.setName ( "SB34 Friends");

the ContactPerson CP1 = the ContactPerson new new ();
cp1.setId (. 1);
the ContactPerson the ContactPerson CP2 new new = ();
cp2.setId (2);

g1.addPerson (CP1);
g1.addPerson (CP2);
Session.update (G1);

the session. getTransaction () commit ();.
find a collection of saved things inside us is whether he, so inverse = true no matter in this set there are no data
public class Group {
Private int the above mentioned id;
Private String name;
Private the Set <ContactPerson> persons = new HashSet <ContactPerson> () ;
I will not go inside the synchronized set of data when stored and updated on the inside when the set of data does not exist or does not matter and I do not care

Guess you like

Origin www.cnblogs.com/Jeely/p/11697705.html