Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hibernate many-to-many relational mapping

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Students and courses are a many-to-many mapping, so how to configure many-to-many relationships in hibernate?

And something to pay attention to? A simple test.

Build a table

Entity

Profile and Mappin

test

one. Use oracle to create a table sql

Create table students (id number (7) primary key, name nvarchar2 (20), age number (2)) create table course (id number (7) primary key, name nvarchar2 (20), time nvarchar2 (20)) create table st_cou (st_id references students (id), cou_id references course (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 = new HashSet (); public 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 getCourse () {return course;} public void setCourse (Set course) {this.course = course;} public Students (Integer id, String name, Integer age) {super (); this.id = id; this.name = name This.age = age;} public Students () {super (); @ Override public 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 = new HashSet (); public 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 getStudents () {return students;} public void setStudents (Set 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 (); @ Override public String toString () {return "Course [id=" + id + ", name=" + name + ", time=" + time + "]";}}

4.hibernate.cfg.xml configuration

Oracle.jdbc.OracleDriver jdbc:oracle:thin:@localhost:1521:xe zt zt org.hibernate.dialect.Oracle10gDialect true true thread

Course.hbm.xmc mapping configuration

5. test

Query and update test strategy

The query can be checked directly, after updating the query, after modifying the value, it can be updated.

Insertion test, cascaded insertion

@ Test public void inset () {Session session = HibUtil.getSession (); Transaction tx = session.beginTransaction (); Course course = new Course (null, "geography", "appearance"); Students students = new Students (null, "balls", 18) Students.getCourse (). Add (course); / / course.getStudents (). Add (students); this sentence does not need to write session.save (students); tx.commit ();}

Many-to-many, insert, as long as Party B carries out maintenance, if both parties write

Students.getCourse () .add (course); course.getStudents () .add (students)

Then in the insertion, and then in the association table, since Party B has maintained, the association relationship already exists at this time, and at this time, the other party has carried out maintenance

Well, since the association already exists, it will be reported to violate the unique constraint condition at this time.

Delete test

@ Test public 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, enter the database query and find that as long as it exists in the associated table, the relevant students and courses will be deleted.

In the configuration, we have configured that the cascading level is all. This permission is too large, so use it with caution. Cascading level changed to save-update

6. Be careful

1. In the entity, we write that one side of the relationship has

Private Set course = new HashSet ()

Why new HashSet ()

When we insert a single table, if there is no new HashSet ()

Students students = (Students) session.get (Students.class, 1); Course course = new Course (null, "geography", "appearance"); where course.getStudents () is a null and null object is used. Add would be a null pointer course.getStudents (). Add (students)

two。 Cascade relation

In the association relationship, the relationship should be used carefully to avoid loss of data operation on the other side.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report