1. 程式人生 > >hibernate 一對多 關系映射

hibernate 一對多 關系映射

hibernate 一對多映射配置

在關系中,或多或少的表之間有復雜的關聯關系

這裏測試 一對多 關聯關系的配置


  1. 開發步驟就不多說了。直接看一個示例。


users 用戶 和 shop商品訂單表

分析:一個用戶可以有多個商品訂單,一個訂單只屬於一個用戶。

這樣就構成了一對多的關聯關系。


這裏用的是oracle 數據庫

users 和 shop 數據庫表

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 實體

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 實體

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 不維護關系
		 -->
		<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添加配置,保證事務中公用義個session -->
       <property name="current_session_context_class">thread</property>
        
        <!-- 註冊 -->
        <mapping resource="shop.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5.測試 級聯插入測試

@Test
	public void insert(){
		Session session = HibUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		Users users = new Users(null, "苦力強", "kuliqiang", "zg");
		Shop shop = new Shop(null, "橙子", 2800);
		
		users.getShop().add(shop);
		shop.setUsers(users);
		
		session.save(users);	
		
		tx.commit();
	}

記得要設置級聯關系。

測試 查詢

@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();
	}


更新和刪除就不測試了,更新先查詢出來,再修改值,然後更新就可以了。

刪除直接查詢出來,刪除就可以了。

本文出自 “永恒之光” 博客,請務必保留此出處http://zhuws.blog.51cto.com/11134439/1940599

hibernate 一對多 關系映射