Java programmers from the stupid bird to the rookie (58) Hibernate (ix) Hibernate a pair of relationship mapping __java

Source: Internet
Author: User
Tags generator rollback



A one-to-one relationship mapping is a reference that both sides of the relationship contain each other. In fact, a one-to-one relationship in life is also very common, such as people and identity cards, students and school numbers, are one-to-one relationship mapping, one-to-one mapping into one-way and two-way, there is no kind of relationship mapping can be divided into primary Key association mapping, unique Foreign Key association mapping.

One: Primary Key Association Mappings

General one-to-one primary Key association mappings use an identifier for another associated object through the foreign primary key generator. Commonly used in conjunction with <one-to-one>. A pair of primary key association mappings: make the primary keys for two entities the same, so you don't need to add extra fields. There are some drawbacks to this correlation mapping: one-way one-to-one primary key association is actually a lot more restrictive, because you only have idcard inserted. person. Let's take a look at the specific example:

Based on the relational class diagram above, let's look at the definition of the entity class

Entity Idcard.java

public class Idcard

{

      private String ID;

      private int number;

      private person person;

}


Entity: Person.java

public class person

{

      private String ID;

      private String name;

      Private Idcard Idcard;

}

Idcard configuration file:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
 "-//hibernate/hibernate mapping dtd3.0//en"
 "http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">


configuration file for Person:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
 "-//hibernate/hibernate mapping dtd3.0//en"
 "http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

Note: The one-to-one label tells Hibernate to load the reference object according to the primary key, to take the primary key in the person to the Idcard table, and then to load the information into the reference object with a one-to-one primary key constraint, then the constrained attribute must be set. Indicates that the current primary key is referenced as a foreign key by default to ask cascading properties in one-to-one primary key association mappings


After the configuration, we will look at specific additions and deletions to check operations:


Person person = Newperson ();

            Person.setname ("Zhangsan");

            Idcard Idcard = new Idcard ();

            Idcard.setnumber (987654);

            Person.setidcard (Idcard);

            Idcard.setperson (person);

            Session session = Sessionfactory.opensession ();

     

            Transaction tx = NULL;        
                  try {tx =session.begintransaction ();
                  Session.save (person);
            Tx.commit ();
                  catch (Exception ex) {ex.printstacktrace ();

                  if (null!= tx) {tx.rollback ();
            finally {session.close (); }//---------------------------------------------------//Session session = Sessionfactory.ope
Nsession ();
Transaction tx = NULL; Person person =Null Try//{/TX =session.begintransaction ();//person = [person] session.
Get (Person.class, "402881ec2ebd7e77012ebd7e79e40001");
Tx.commit ();                      }//catch (Exception ex)//{//if (null!= tx)//{/
Tx.rollback ();          /////finally//{//Session.close ();//}//
System.out.println (Person.getname ());          

            System.out.println (Person.getidcard (). GetNumber ());
-----------------------------------------//Session session = Sessionfactory.opensession ();
Transaction tx = NULL;
person person = null;  Try//{/TX =session.begintransaction ();//// = (person) session.get (Person.class, "402881ec2ebd7e77012ebd7e79e40001 ");    
Person.setname ("Lisi");
Tx.commit ();                      }//catch (Exception ex)//{//if (null!= tx)//{/
Tx.rollback ();          /////finally//{//Session.close ();//}//         
            System.out.println (Person.getname ());
-----------------------------------------//Session session = Sessionfactory.opensession ();
Transaction tx = NULL;
person person = null;  
Try//{/TX =session.begintransaction ();      
person = (person) session.get (Person.class, "402881ec2ebd7e77012ebd7e79e40001");                
Session.delete (person);
Tx.commit ();              }//catch (Exception ex)//{//if (null!= TX)//  {//Tx.rollback ();//}//}//finally//{//
Session.close (); //          }


Through the execution of the query, we can find that hibernate's one-to-one default implementation of the search method is the external connection search, if we do not want to use the external link retrieval, we can set the one-to-one fetch property, he has two values, one is select, one is Jion. I think we can literally guess the way they are retrieved.

Hibernate a pair can also set deferred loading, one-to-one by default, and if you need to use deferred loading, you need to set the constrained property to true in the one-to-one element. and set the Lazy property in the class element of the party to be loaded to true (or not set, because the default value of the property is true). The left outer join is used by default on one-to-one loading and can be modified for select by modifying the Fetch property to send a single SELECT statement at a time.

Unique Foreign Key Association mapping: In fact it is a one-to-many special case, it is basically the same as one-to-many, but only need to configure a property. It is essentially a pair of many forms of degeneration. Increasing the Unique= "true" attribute in the Many-to-one element becomes one-to-one.

two or one to unique foreign key association mappings--one-way

1. A pair of unique foreign key association mappings is a special case of a many-to-many correlation mapping, which can be done with <many-to-one> tags, specifying multiple ends of the unique=true, which limits the multiplicity of one end to the other, mapping one-to-one unique foreign key associations by this means

2. Domain Model Diagram:


3. Configure

Person.hbm.xml:

<class name= "Com.bjsxt.hibernate.Person" table= "T_person" >

<id name= "id" >

<generator class= ' Native '/>

</id>

<property name= ' name '/> <many-to-one name= ' idcard ' unique= '

true ' >

</class>

IDCard.hbm.xml:

<class name= "Com.bjsxt.hibernate.IdCard" table= "T_idcard" >

<id name= "id" >

<generator class= "Native"/>

</id>

<property name= "Cardno"/>

</class>

three or one to unique foreign key association mappings-bidirectional

1. One-to-one unique Foreign Key association bi-directional, need at the other end (Idcard), add <one-to-one> tag, indicate how hibernate to load its associated objects, by default by the primary key load person, foreign key association mapping, Because two entities are using the relationship of person's foreign key maintenance, you cannot specify a primary key to load a person, but to load it according to the foreign key of someone, the following mapping method is used:

<one-to-one name= "Person" property-ref= "Idcard"/>

2. Domain Model Diagram:


3. Specific configuration:

Person.hbm.xml:

<class name= "Com.bjsxt.hibernate.Person" table= "T_person" >

<id name= "id" >

<generator class= ' Native '/>

</id>

<property name= ' name '/> <many-to-one name= ' idcard ' unique= '

true ' >

</class>

IDCard.hbm.xml

<class name= "Com.bjsxt.hibernate.IdCard" table= "T_idcard" >

<id name= "id" >

<generator class= "Native"/>

</id>

<property name= "Cardno"/> <one-to-one name=

"person" property-ref= " Idcard "/>

</class>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.