您好,登錄后才能下訂單哦!
在關(guān)系中,或多或少的表之間有復(fù)雜的關(guān)聯(lián)關(guān)系
這里測試 一對多 關(guān)聯(lián)關(guān)系的配置
開發(fā)步驟就不多說了。直接看一個示例。
users 用戶 和 shop商品訂單表
分析:一個用戶可以有多個商品訂單,一個訂單只屬于一個用戶。
這樣就構(gòu)成了一對多的關(guān)聯(lián)關(guān)系。
這里用的是oracle 數(shù)據(jù)庫
users 和 shop 數(shù)據(jù)庫表
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 實(shí)體
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> shop = new HashSet<Shop>(); 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<Shop> getShop() { return shop; } public void setShop(Set<Shop> 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 實(shí)體
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映射文件配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.hib.entity.Users" table="users"> <id name="id"> <generator class="increment"> </generator> </id> <property name="name"/> <property name="pass"/> <property name="address"/> <!-- 1的一方配置 invese: true 不維護(hù)關(guān)系 --> <set name="shop" inverse="true" cascade="all"> <!-- sh_id 是下面多一方的外鍵表中字段 --> <key column="us_id"/> <one-to-many class="com.hib.entity.Shop"/> </set> </class> <class name="com.hib.entity.Shop" table="shop"> <id name="id"> <generator class="increment"></generator> </id> <property name="sh_name"/> <property name="price"/> <many-to-one name="users" class="com.hib.entity.Users" column="us_id" cascade="none"/> </class> </hibernate-mapping>
4. 不要忘記注冊。
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.username">ssh</property> <property name="hibernate.connection.password">ssh</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property> <!-- 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 可視化sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql,自動對可視化sql換行,對齊 --> <property name="hibernate.format_sql">true</property> <!-- 為getCurrentSession添加配置,保證事務(wù)中公用義個session --> <property name="current_session_context_class">thread</property> <!-- 注冊 --> <mapping resource="shop.hbm.xml"/> </session-factory> </hibernate-configuration>
5.測試 級聯(lián)插入測試
@Test public void insert(){ Session session = HibUtil.getSession(); Transaction tx = session.beginTransaction(); Users users = new Users(null, "苦力強(qiáng)", "kuliqiang", "zg"); Shop shop = new Shop(null, "橙子", 2800); users.getShop().add(shop); shop.setUsers(users); session.save(users); tx.commit(); }
記得要設(shè)置級聯(lián)關(guān)系。
測試 查詢
@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(); }
更新和刪除就不測試了,更新先查詢出來,再修改值,然后更新就可以了。
刪除直接查詢出來,刪除就可以了。
如果是單向的1對多 上面的可以修改為
users 實(shí)體不改變
shop 中的 users去掉
配置文件中 去掉下面這段配置
<many-to-one name="users" class="com.hib.entity.Users" column="us_id" cascade="none"/>
4. 配置文件中 inverse="true" 去掉,告訴主表,來維護(hù)從表
<set name="shop" inverse="true" cascade="all">
其他的還是一樣操作。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。