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 one-to-many relational mapping

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In a relationship, there are complex relationships between more or less tables

Test the configuration of one-to-many relationships here

I won't say much about the development steps. Look directly at an example.

Users user and shop goods order list

Analysis: a user can have multiple orders for goods, and an order belongs to only one user.

This constitutes an one-to-many relationship.

The oracle database is used here.

Users and shop database tables

Create table users (id number (7) primary key, name nvarchar2 (20), pass nvarchar2 (20), address nvarchar2 (50)) create table shop (id number (7) primary key, price number (7), sh_name nvarchar2 (50), us_id references users (id))

2.users entity

Package com.hib.entity;import java.util.HashSet;import java.util.Set;public class Users {private Integer id; private String name; private String pass; private String address; private Set shop = 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 getPass () {return pass;} public void setPass (String pass) {this.pass = pass } public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public Set getShop () {return shop;} public void setShop (Set shop) {this.shop = shop } public Users (Integer id, String name, String pass, String address) {super (); this.id = id; this.name = name; this.pass = pass; this.address = address;} public Users () {super () @ Override public String toString () {return "Users [id=" + id + ", name=" + name + ", pass=" + pass + ", address=" + address + "]";}}

Shop entity

Package com.hib.entity;public class Shop {private Integer id; private Integer price; private String sh_name; private Users users; public Integer getId () {return id;} public void setId (Integer id) {this.id = id } public String getSh_name () {return sh_name;} public void setSh_name (String sh_name) {this.sh_name = sh_name;} public Integer getPrice () {return price;} public void setPrice (Integer price) {this.price = price } public Users getUsers () {return users;} public void setUsers (Users users) {this.users = users;} public Shop (Integer id, String sh_name, Integer price) {super (); this.id = id This.sh_name = sh_name; this.price = price;} public Shop () {super ();} @ Override public String toString () {return "Shop [id=" + id + ", sh_name=" + sh_name + ", price=" + price + "]" }}

3.shop.hbm.xml Mapping File configuration

4. Don't forget to register.

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

5. Test cascade insertion test

@ Test public void insert () {Session session = HibUtil.getSession (); Transaction tx = session.beginTransaction (); Users users = new Users (null, "coolie", "kuliqiang", "zg"); Shop shop = new Shop (null, "Orange", 2800) Users.getShop (). Add (shop); shop.setUsers (users); session.save (users); tx.commit ();}

Remember to set up cascading relationships.

Test query

@ Test public void query () {Session session = HibUtil.getSession (); Transaction tx = session.beginTransaction (); Users users = (Users) session.get (Users.class, 1); for (Shop shop: users.getShop ()) {System.out.println (shop) } System.out.println (users); tx.commit ();}

Update and delete will not be tested, the update will be queried first, then the value will be modified, and then it will be updated.

Delete directly query out, delete it.

If it is an one-way one-to-many, the above can be changed to

Users entity does not change

Remove users from shop

Remove the following configuration from the configuration file

4. Remove the inverse= "true" from the configuration file and tell the master table to maintain the slave table

Everything else works the same way.

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