Student and curriculum is a many-to-many relationship mapping, then hibernate, how to configure the many-to-many relationship?
And some things to note? Under simple test.
Build table
Entity
Configuration files and Mappings
Test
1. Using Oracle, Build table SQL
CREATE TABLE students (ID number (7) primary key, name NVARCHAR2, age number (2)) CREATE TABLE course (ID number (7) Prima Ry Key, name Nvarchar2, Time nvarchar2 () CREATE TABLE St_cou (ST_ID references students (ID), cou_id references Cou RSE (ID), primary key (ST_ID,COU_ID))
2. Students entity
Package com.hibernate.entity;import java.util.hashset;import java.util.set;public class Students {private Integer id;private String name;private Integer age; Private set<course> course = new hashset<course> ();p Ublic integer getid () {return id;} Public void setid (Integer id) {this.id = id;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public integer getage () {return age;} Public void setage (Integer age) {this.age = age;} Public set<course> getcourse () {return course;} Public void setcourse (Set<course> course) {this.course = course;} Public students (Integer id, string name, integer age) {super (); this.id =&nbSp;id;this.name = name;this.age = age;} Public students () {super ();} @Overridepublic string tostring () {return "students [id=" + id + ", name=" + name + ", age=" + age + ", course=" + course + "]";}}
3. Course entity
Package com.hibernate.entity;import java.util.hashset;import java.util.set;public class Course {private Integer id;private String name;private String time; Private set<students> students = new hashset<students> ();p ublic Integer getid () {return id;} Public void setid (Integer id) {this.id = id;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public string gettime () {return time;} Public void settime (String time) {this.time = time;} Public set<students> getstudents () {return students;} Public void setstudents (set<students> students) {this.students = students;} Public course (integer id, string name, string time) {supER (); this.id = id;this.name = name;this.time = time;} Public course () {super ();} @Overridepublic string tostring () {return "course [id=" + id + ", name= " + name + ", time= " + time + "] ";}}
4.hibernate.cfg.xml Configuration
<! doctype hibernate-configuration public "-//Hibernate/ hibernate configuration dtd 3.0//en " "/http Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd "> COURSE.HBM.XMC Mapping Configuration
<?xml version= "1.0"? ><! doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en " " HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD "> 5. Testing
Query and update tests slightly
Query directly check can be, after updating the query, after modifying the value, the update is ready.
Insert Test, CASCADE Insert
@Testpublic void inset () {Session session = Hibutil.getsession (); Transaction tx = Session.begintransaction (); Course Course = new Course (NULL, "Geography", "face"); Students Students = new Students (null, "balls"), Students.getcourse (). Add (course);//Course.getstudents (). Add ( students); This sentence does not need to write session.save (students); Tx.commit ();}
Many-to-many, inserted, as long as the maintenance of party B can be, if two parties are written
Students.getcourse (). Add (course); Course.getstudents (). Add (students);
Then in the Insert, and then related to the table, because party B has been maintained, there is already an association, this time, the other party has been maintained
Then, because there is already an association, the violation of the unique constraint is reported
Delete Test
@Testpublic void Delete () {Session session = Hibutil.getsession (); Transaction tx = Session.begintransaction (); Students Students = (Students) session.get (Students.class, 1); Session.delete (Students); Tx.commit ();}
After execution, into the database query, found that as long as it exists in the association table, the relevant students and courses will be deleted.
Configuration, we configured the Cascade level is all this permission is too large, use caution. Cascade level modified to Save-update
6. Note
1. In the entity we write the relationship one side has
Private set<course> Course = new hashset<course> ();
Why new Hashset<course> ();
When we do a single insert table, if there is no new hashset<course> ();
Students Students = (Students) session.get (Students.class, 1); Course Course = new Course (NULL, "Geography", "face"); here the course.getstudents () is null and the null object. Add will be a null pointer course.getstudents (). Add ( Students);
2. Cascading relationships
In the association relationship, the relationship should be used with caution to avoid loss of data operation on the other side.
This article is from the "Eternal Light" blog, please be sure to keep this source http://zhuws.blog.51cto.com/11134439/1940750
Hibernate many-to-many relationship mappings