"Hibernate framework" Association mappings (one-to-one correlation mappings)

Source: Internet
Author: User
Tags commit generator valid
first, the arrangement of ideas

Hibernate's associated mappings, directly above:


This diagram is a general idea of the whole hibernate relational mapping.
second, the noun explanation 1, one-way association: Very simple, is that one object depends on another object.
2. Two-Way Association: Two objects depend on each other.
three or one to one (one-to-one) association mappings so-called a-plain English understanding is that an object has some kind of supplementary item that can and can only have one piece. For example, as a student, a student can only have a valid student ID, and an upright Chinese citizen can only have a valid identity card. This is one-to-one. Next, let's take the example of an in-service student and say one-to-one mapping.
There are two ways in hibernate to achieve one-to-one mapping, namely:
1, the primary key association;
2, unique foreign Key association.
four, each introduction 4.1. One-way correlation mapping 4.1.1, one-to-one unidirectional association mappings--PRIMARY Key Association
Core: One object depends on another object. For example: According to the student, find the corresponding valid student card.

Table Design:



Po Object Design:
Studentcard.java:

public class Studentcard {
	private int id;
	Private String Cardno;
	Getter, setter Method
}
Student.java:
public class Student {
	private int id;
	private String name;
	Private Studentcard Studentcard;
	Getter, setter Method	
}
Map file:
StudentCard.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Student.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Analysis: Core <one-to-one name= "Studentcard" constrained= "true"/&GT;, which represents a student corresponding to one studentcard. Constrained= "True" indicates that there is a foreign key on the primary key of the T_student table that points to the primary key of the associated table (T_studentcard), the constraint is created on the table t_student, and the constraint T_ The ID of the student can only be the same as the primary key of Studentcard, that is, the ID on student is both the primary key and the foreign key.
Test:
@Test public
void Testsavestudent () {
	Session session = Hibernateutils.getsession ();
	Transaction tx = Session.begintransaction ();

	Studentcard Studentcard = new Studentcard ();
	Studentcard.setcardno ("12050242013");
	Student Student = new Student ();
	Student.setname ("David");
	Student.setstudentcard (Studentcard);
	Session.save (student);

	Tx.commit ();
	Session.close ();
}
At this point, a studentcard message is inserted before inserting the student information:

4.1.2, one-way, single-direction association mappings-Unique Foreign Key Association
Core: The only foreign key association is to add a foreign key to an object in a one-to-one relationship. Example: Add a foreign key to the student table, point to the primary key of the Studentcard, and the foreign key is unique, so that it achieves the effect of one-on mapping.
Po Object Design:
Studentcard.java:

public class Studentcard {
	private int id;
	Private String Cardno;
	Getter, setter Method
}
Student.java:
public class Student {
	private int id;
	private String name;
	Private Studentcard Studentcard;
	Getter, setter Method
}
StudentCard.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Student.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Analysis:
Here and the primary Key association is also a bit different, T_student's primary key generation strategy is native, no longer in the form associated with the T_studentcard primary key, with the <many-to-one> tag and t_idcard to establish a many-to-one relationship, This allows you to generate a foreign key associated with the T_studentcard primary key in T_student.
At this time if also use the above test code to test, will be error: "Org.hibernate.TransientObjectException:object references an unsaved transient instance"
Because T_student's primary key generation strategy is native, and t_studentcard is irrelevant, and Studentcard does not become transientobject, we must first turn Studentcard into a persistent object:
@Test public
void Testsavestudent () {

	Session session = Hibernateutils.getsession ();
	Transaction tx = Session.begintransaction ();

	Studentcard Studentcard = new Studentcard ();
	Studentcard.setcardno ("12050242013");
	Session.save (Studentcard);
	Student Student = new Student ();
	Student.setname ("David");
	Student.setstudentcard (Studentcard);
	Session.save (student);

	Tx.commit ();
	Session.close ();
}
4.2. One-to-one bidirectional correlation mapping4.2.1 One-to-one bidirectional correlation mapping-primary Key Association
Core: Between two objects, interdependent
Po Object
Studentcard.java
public class Studentcard {
	private int id;
	Private String Cardno;
	Private Student Student;
	Getter, setter Method
}
Student.java
public class Student {
	private int id;
	private String name;
	Private Studentcard Studentcard;
	Getter, setter Method
}
Student.java configuration file:
StudentCard.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Student.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Test as above, we find Student, Student.studentcard can obtain student ID information. When we find Studentcard, studentcard.student can get student information. But it is precisely because of this relationship between them that there is no mutual restraint, so when we execute:
@Test public
void Testsavestudentcard () {

	Session session = Hibernateutils.getsession ();
	Transaction tx = Session.begintransaction ();

	Student Student = new Student ();
	Student.setname ("Kennth");
	Studentcard Studentcard = new Studentcard ();
	Studentcard.setstudent (student);
	
	Session.save (Studentcard);
	Tx.commit ();
	Session.close ();
}
Session will only save (Studentcard), but not save (student);
4.2.2 One-to-one bidirectional correlation mapping--Unique foreign Key Association
Directly on the configuration:
Po Object Design:
Studentcard.java

public class Studentcard {
	private int id;
	Private String Cardno;
	Private Student Student;
	Getter, setter Method
}
Student.java
public class Student {
	private int id;
	private String name;
	Private Studentcard Studentcard;
	Getter, setter Method
}
Configuration file:
StudentCard.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Student.hbm.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

An ID card corresponds to only one citizen, so with the <one-to-one> tag, property-ref= "Studentcard" refers to T_studentcard's primary key corresponding to the Studentcard field in T_student.
To this, about the one-to-one mapping knowledge of the end, but you have to know that the only foreign key association is a more than a special case of a correlation, so when our needs change, ask for a pair of one to become a lot of one, what should we do? In fact, it is very simple, directly the only constraint of the foreign key to kill on it, is not a lot of convenience.
Transferred from: http://www.cnblogs.com/DoubleEggs/p/6254922.html

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.