Link ing in Hibernate
Introduction to advanced ORM
Advanced ORM HibernateIntroduction and framework
Advanced ORM: three objects of Hibernate
Analysis of the three states of objects in Hibernate
One-to-one relationship ing in Hibernate
Ing is a very important part in hibernate. Through ing, programmers can no longer think about complicated SQL statements, but focus more on the implementation of business logic. Ing is completed through an xml configuration file and we can modify it! Next let's take a look at how to complete the ing!
Single Table ing
Each object corresponds to a table and has no association with other entities. This is the simplest way. Let's take a look at how to map objects to a database. Code of our entity User
/*** User entity ** @ author hongjie **/public class User {// primary key private int id; // username private String username; // password private String passwords; public int getId () {return id;} public void setId (intid) {this. id = id;} public String getUsername () {return username;} public void setUsername (Stringusername) {this. username = username;} public String getPasswords () {return passwords;} public void setPasswords (Stringpasswords) {this. passwords = passwords ;}}
User. hbm. xml code of the ing File
With these two simple configuration files, we can map our entities to the database! This is also the simplest ing method. However, in reality, few entities are irrelevant.
One-to-one relationship ing
There is also a one-to-one relationship. For example, a user can only have one ID card information. We need to store the ID card information separately, so that our user entity and ID card entity are one-to-one, there must be someone first, and then there is ID card information. Therefore, the user should give priority to the primary account. The ID card information is slave, and the ID card information of the user maintainer. The one-to-one relationship of hibernate has two forms: one is the primary key sharing method, and the other is the unique foreign key sharing method .. How can we express this relationship! Take a look at the code.
The Shared primary key method, as its name implies, means that two records of a person use the same primary key. When a user is created and the ID card information is created, the user automatically obtains the primary key of the user information. In this way, you can use the same primary key. Let's take a look at the code below,
Package com. tgb. domain;/*** User entity * @ author hongjie **/public class User {// primary key private int id; // username private String username; // password private String passwords; // Card information private card Card; public card getCard () {return Card;} public void setCard (card Card card) {this. card = card;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPasswords () {return passwords;} public void setPasswords (String passwords) {this. passwords = passwords ;}}
Public class Card {// primary key private int cardId; // ID Card information private String cardInfo; // User information private user User; public user getUser () {return User ;} public void setUser (User user) {this. user = user;} public int getCardId () {return cardId;} public void setCardId (int cardId) {this. cardId = cardId;} public String getCardInfo () {return cardInfo;} public void setCardInfo (String cardInfo) {this. cardInfo = cardInfo ;}}The above is the code of the two entities. We can see that there is a card reference in the user, and there is also a user reference in the card. This is a bidirectional Association, based on any of the information, you can obtain another information. Let's take a look at the configuration file code.
User
Through the configuration file above, we have created a one-to-one unique primary key Association ing. Next we will take a look at another one-to-one implementation method for foreign key Association. The foreign key is associated with our pojo class, or the two classes above. We just modified the configuration file. The configuration file is as follows:
User
We can see that we simply modified the configuration file and changed it into a unique foreign key constraint. In this way, you can also implement one-to-one ing. The next blog will explain many-to-one ing for you.