Deep parsing of a one-to-one association mapping in the Java Hibernate framework _java

Source: Internet
Author: User
Tags generator

As an ORM framework, hibernate certainly needs to meet our need to relate tables to tables. The implementation of Hibernate in association method is very simple. Let's take a look at a one-to-one approach:
Not much to say, we go directly to the code:
Two entity classes, Tuser and Tpassport:

public class TUser implements serializable{ 
 
 private static final long serialversionuid = 1L; 
 private int id; 
 private int age; 
 private String name; 
 private Tpassport passport; 
  Omit Get/set method 
} public 
class Tpassport implements serializable{ 
 
 private static final long Serialversionuid = 1L; 
 private int id; 
 Private String serial; 
 private int expiry; 
 Private TUser user; 
  Omit Get/set Method 
} 

Let's look at what the mapping file is different:

 
 

Here we see a new tag, one-to-one, which shows that the current class is one-to-one with the corresponding class, Cascade is a cascading relationship, and all shows that no matter what the situation is cascaded, that is, when the Tuser class is operated on, Tpassport also do the appropriate action, Outer-join refers to whether to use the outer JOIN statement.
Let's look at another tpassport mapping file:

 
 


Here we focus on the generator class value, which indicates that the reference foreign key is referenced by the foreign, and which one is specified by the Param, which indicates the ID of the reference user class. The one-to-one tag has a constrained attribute that tells Hibernate that the current class has a foreign key constraint, that is, the ID of the current class is generated based on the Tuser ID.
Here we go directly to the test class, this time the test class does not use JUnit but the direct Main method:

public static void Main (string[] args) { 
   
  Configuration cfg = new Configuration (). Configure (); 
  Sessionfactory sessionfactory = Cfg.buildsessionfactory (); 
  Session session = Sessionfactory.opensession (); 
   
  Session.begintransaction (); 
   
  TUser user = new TUser (); 
  User.setage (a); 
  User.setname ("Shuntest"); 
   
  Tpassport Passport = new Tpassport (); 
  Passport.setexpiry (a); 
  Passport.setserial ("123123123"); 
   
  Passport.setuser (user); 
  User.setpassport (passport); 
   
  Session.save (user); 
   
  Session.gettransaction (). commit (); 
   
  

The code is very simple, do not say. We mainly look here:

Session.save (user); 

Here's why we call only one save, because in our tuser mapping file, the Cascade attribute, which is set to all, means that when we save, update, delete, and so on Tuser, Tpassport will do the same. So we don't have to write Session.save (passport) here. We see backstage:

Hibernate:insert into USER4 (name, age) VALUES (?,?) 
Hibernate: It prints out two statements, proving that Hibernate sure helped us do the job.


Let's take another test class and test the query:
public static void Main (string[] args) { 
  Configuration cfg = new Configuration (). Configure (); 
  Sessionfactory sessionfactory = Cfg.buildsessionfactory (); 
  Session session = Sessionfactory.opensession (); 
   
  TUser user = (TUser) session.load (tuser.class,new Integer (3)); 
  System.out.println (User.getname () + ":" +user.getpassport (). getserial ()); 
   
 } 
Here we query out the Tuser class and fetch the Tpassport object.
We can see that the hibernate SQL statement is:
Copy Code code as follows:

Hibernate:
Select Tuser0_.id as id0_1_, tuser0_.name as name0_1_, tuser0_.age as age0_1_, tpassport1_.id as id1_0_, Tpassport1_.seria L as serial1_0_, Tpassport1_.expiry as expiry1_0_ from USER4 tuser0_ left outer join Passport4 tpassport1_ on tuser0_.id=t Passport1_.id where tuser0_.id=?

We see a left outer join in the statement because we set the outer-join= "true" in the one-to-one, we try to change it to false and see the SQL statement as follows:
Copy Code code as follows:

Hibernate:
Select Tuser0_.id as id0_0_, tuser0_.name as name0_0_, tuser0_.age as age0_0_ from USER4 tuser0_ where tuser0_.id=?
Hibernate:
Select Tpassport0_.id as id1_0_, tpassport0_.serial as serial1_0_, Tpassport0_.expiry as expiry1_0_ from Passport4 rt0_ where tpassport0_.id=?


Now is divided into two to check, according to the first identified ID and then to the second detection.

Perhaps a lot of people will ask why the test without tpassport find Tuser, in fact, because they are one-to-one relationship, who check who are the same.

FOREIGN Key Association
Now let's look at a one-to-one association that is associated with a foreign key.
Or is it consistent with the direct example: We wrote two entity classes, Tgroup and Tuser

public class Tgroup implements serializable{ 
 
 private static final long serialversionuid = 1L; 
 private int id; 
 private String name; 
 Private TUser user; 
  Omit Get/set method 
} public 
class TUser implements serializable{ 
 
 private static final long serialversionuid = 1L; 
 private int id; 
 private int age; 
 private String name; 
 Private Tgroup Group; 
  Omit Get/set Method 
  
} 

Entity class finished We'll take a look at the mapping file:

 
 

Here we see the use of Many-to-one labels instead of one-to-one, why?
It used to be a lot of attention, it will work anyway, but this time read Xia Xin's book finally understand, in fact, this through the foreign key to the association is just a special way of one, we through the unique= "true" to limit it must have only one, that is, to achieve a one-to-one association.
Next we look at the Tgroup mapping file:

 
 

Here, notice that we're using the one-to-one again to show that the current entity and Tuser are one-to-one relationships where we don't need to many-to-one, but instead we specify which property in the Tuser entity to associate the current class Tgroup by one-to-one. Here we specify that the Tuser is associated through the group attribute and the Tuser. PROPERTY-REF Specifies which attribute to associate with.
Here we look at the test class:

public class Hibernatetest {public 
 
 static void Main (string[] args) { 
 
  Configuration cfg = new Configuration (). conf Igure (); 
  Sessionfactory sessionfactory = Cfg.buildsessionfactory (); 
  Session session = Sessionfactory.opensession (); 
   
  Session.begintransaction (); 
   
  Tgroup Group = new Tgroup (); 
  Group.setname ("Testgroup"); 
   
  TUser user = new TUser (); 
  User.setage (a); 
  User.setname ("test"); 
   
  User.setgroup (group); 
  Group.setuser (user); 
   
  Session.save (group); 
  Session.save (user); 
   
  Session.gettransaction (). commit (); 
  Session.close (); 
 } 
 
 

Note that this time we need to save two times in our code because they correspond to each other, and only one of them will not operate on the other. So we need to invoke two saved operations. The final submission.
Hibernate Print out statements:

Hibernate:insert into GROUP5 (name) VALUES (?) 
Hibernate:insert into USER5 (name, age, group_id) VALUES (?,?,?) 

This means that we have correctly deposited two object values.

We write more than one test class to query:

public static void Main (string[] args) { 
 
  Configuration cfg = new Configuration (). Configure (); 
  Sessionfactory sessionfactory = Cfg.buildsessionfactory (); 
  Session session = Sessionfactory.opensession (); 
   
  TUser user = (TUser) session.load (tuser.class,new Integer (1)); 
  System.out.println ("From User get Group:" +user.getgroup (). GetName ()); 
   
   
  Tgroup Group = (tgroup) session.load (tgroup.class,new Integer (1)); 
  System.out.println ("From Group get User:" + group.getuser (). GetName ()); 
   
  Session.close (); 
   
 } 

We can all get the right results, which shows that we can get the value of each other through two objects and achieve our goal.
The Tgroup and Tuser used in this example are just examples, in fact, in real life, user typically corresponds to multiple group.

Related Article

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.