In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
I have been in contact with Hibernate for a short time, and I feel more and more that Hibernate is a magical thing. Why do you say so? Because you can not understand a line of sql, directly object-oriented, you can directly save the data to the database!
You can also save an object, and then save all the data related to it to the database at once, for example, you only need to save the class object. you can form a pile of records of the class information and all the students in the class in the database.
And you don't need to write sqlcards!
Is it amazing? Anyway, baby, I was stunned.
Let's talk about the specific code implementation.
First of all, let's talk about a simple one-way one-to-many case (taking classes and students as cases).
As we all know, Hibernate uses an object-oriented idea. If we want to associate with a database, we must first have a corresponding entity class.
For example, I have a student object and a class object, and the details of the student table and class table in the database are as follows:
Integer sid; String sname; String sex; .sname = .sex = .sid = .sname = .sex =
Package entity;/** class table * / import java.io.Serializable;import java.util.HashSet;import java.util.List;import java.util.Set;public class Grade implements Serializable {private Integer gid;// class number private String gname;// class name private String gdesc;// class description public Grade () {} public Grade (String gname, String gdesc) {this.gname = gname; this.gdesc = gdesc } public Integer getGid () {return gid;} public void setGid (Integer gid) {this.gid = gid;} public String getGname () {return gname;} public void setGname (String gname) {this.gname = gname;} public String getGdesc () {return gdesc;} public void setGdesc (String gdesc) {this.gdesc = gdesc }}
One-to-many should be easier to understand, because we can understand that a class can correspond to multiple students, which is one-to-many. Since a class corresponds to multiple students, can we download it from the physical class of the class?
What about adding a student collection and? Does this better reflect the one-to-many relationship? So we have the following transformation download for the class entity
Package entity;import java.io.Serializable;import java.util.HashSet;import java.util.List;import java.util.Set;public class Grade implements Serializable {private Integer gid;// grade number private String gname;// grade name private String gdesc;// grade description / / add a class collection of students private Set stus=new HashSet (); public Set getStus () {return stus } public void setStus (Set stus) {this.stus = stus;} public Grade () {} public Grade (String gname, String gdesc) {this.gname = gname; this.gdesc = gdesc;} public Integer getGid () {return gid;} public void setGid (Integer gid) {this.gid = gid } public String getGname () {return gname;} public void setGname (String gname) {this.gname = gname;} public String getGdesc () {return gdesc;} public void setGdesc (String gdesc) {this.gdesc = gdesc;}}
When the entity class is finished, we should write the most critical configuration file, that is, the mapping file. Download (Grade.hbm.xml)
In this way, we have completed the one-to-many configuration, at this point, we do not have to do anything with Student.hbm.xml, we can test the download
/ / one-way one-to-many case (one class corresponds to multiple students) public static void DOneToManyAdd () {/ / prepare session Session session=HibernateUtil.currentSession (); / / start transaction Transaction tx = session.beginTransaction (); / / create a class Grade grade=new Grade ("S1261", "invincible class Y1261") / prepare several students Student stu1=new Student ("slightly Hot Snow", "female"); Student stu2=new Student ("Paris Rainy season", "male"); / / set up the students in the class grade.getStus (). Add (stu1); grade.getStus (). Add (stu2); / / Save session.save (grade); session.save (stu1) Session.save (stu2); / / commit transaction tx.commit (); / / close connection HibernateUtil.closeSession ();}
After executing this code, you can see the following information on the console
At this point, you have the following information in your database
As you can see from the above test code, I did not manually specify the class of the students, but because of the mapping file, Hibernate will automatically retrieve the class and send sql statements to the database for persistence.
This is the power of Hibernate, of course, this is only the simplest example, let me take a look at a more interesting example! download
One-way many-to-one Relational Mapping of Hibernate Relational Mapping II
Many-to-one mapping is also easy to understand, for example, multiple students can be in the same class at the same time, which is an one-way many-to-one relationship, so we can think of adding a class attribute to the student table.
Package entity;import java.io.Serializable;public class Student implements Serializable {private Integer sid;// student number private String sname;// student name private String sex;// student gender / / create a class private Grade grade; public Grade getGrade () {return grade;} public void setGrade (Grade grade) {this.grade = grade } public Student () {} public Student (String sname, String sex) {this.sname = sname; this.sex = sex;} public Integer getSid () {return sid;} public void setSid (Integer sid) {this.sid = sid;} public String getSname () {return sname } public void setSname (String sname) {this.sname = sname;} public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;}}
Because it is one-way many-to-one, we only need to modify it in the configuration file of the student side, and the configuration file of the class side remains original (that is, when there is no set tag)
Similarly, we do an one-way many-to-one add operation download
/ one-way many-to-one add case (multiple students correspond to one class) public static void DManyToOneAdd () {/ / prepare session Session session=HibernateUtil.currentSession (); / / start transaction Transaction tx = session.beginTransaction (); / / create a class Grade grade=new Grade ("S2222 Class", "S2222 Class") / prepare several students Student stu1=new Student ("Enen", "male"); Student stu2=new Student ("hehe", "female"); / / set up the class stu1.setGrade (grade); stu2.setGrade (grade); / / Save session.save (grade); session.save (stu1); session.save (stu2) / / commit transaction tx.commit (); / / close connection HibernateUtil.closeSession ();}
Attention! The sql statements generated by Hibernate at this time are different from those generated by one-to-many!
There are also corresponding records in the database.
After the demonstration of the above two cases, some students may have questions. Since multiple students can belong to a class and a class can have multiple students, what can be the relationship between them?
At this point, we can set it to a two-way one-to-many relationship. Because there is a two-way relationship between the class and the students, and there are multiple students in a class.
At this point, our complete configuration file is the sum of the above two.
Student.hbm.xml
Grade.hbm.xml
test data
/ / two-way add case private static void SAdd () {/ / prepare session Session session=HibernateUtil.currentSession (); / / Open transaction Transaction tx = session.beginTransaction (); / / create a class Grade grade=new Grade ("S2222 class", "S2222 class"); / / prepare several student Student stu1=new Student ("Paris rainy season", "male") Student stu2=new Student ("slightly Hot Snow", "Girl"); / / set the students under the class grade.getStus (). Add (stu1); grade.getStus (). Add (stu2); / / set classes for students stu1.setGrade (grade); stu2.setGrade (grade); / / Save session.save (grade); session.save (stu1) Session.save (stu2); / / commit transaction tx.commit (); / / close connection HibernateUtil.closeSession ();}
Careful students will find that when I execute the above code, the effect is the same as setting many-to-one and one-to-many, and it is more tedious and complicated, so this is not the advantage of a two-way relationship.
Here we are going to use the properties of cascade (cascading). After setting the properties of cascading, because of the two-way relationship, when you only add classes, Hibernate will automatically add students under the class.
Student.hbm.xml download
Grade.hbm.xml
So when we set the properties of the cascading, the test code is as follows
/ / two-way add case (add class automatically add students under class) private static void SAdd () {/ / prepare session Session session=HibernateUtil.currentSession (); / / start transaction Transaction tx = session.beginTransaction (); / / create a class Grade grade=new Grade ("class S2222", "class S2222") / prepare several students Student stu1=new Student ("Paris Rainy season", "male"); Student stu2=new Student ("slightly Hot Snow", "female"); / / set up the students under the class grade.getStus (). Add (stu1); grade.getStus (). Add (stu2); / / set up classes stu1.setGrade (grade) for students; stu2.setGrade (grade) / / Save (set cascading attribute to automatically associate students in this class) session.save (grade); / / commit transaction tx.commit (); / / close connection HibernateUtil.closeSession ();}
In this way, we only need to write save (grade) to save the class, and Hibernate will generate the following code
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.