1. 程式人生 > >Hibernate之複合主鍵對映

Hibernate之複合主鍵對映

一,概述

複合主鍵也是開發中經常遇到的需求,這篇部落格就是關於複合主鍵對映,開始吧!

二,實體類準備

1)複合主鍵類

package com.bighuan.d_compositeKey;

import java.io.Serializable;

/**
 * 複合主鍵類:使用者名稱和地址唯一標識一條記錄
 * 
 * @author bighuan
 * 
 */
public class CompositeKeys implements Serializable {

	private String userName;
	private String address;

	public CompositeKeys() {

	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}
2)表對應的實體類User.java
package com.bighuan.d_compositeKey;

public class User {

	public User() {

	}

	private int age;

	private CompositeKeys keys;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public CompositeKeys getKeys() {
		return keys;
	}

	public void setKeys(CompositeKeys keys) {
		this.keys = keys;
	}

}

三,配置檔案準備

1)User.java對應的對映檔案User.hbm.xml

<?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">

<hibernate-mapping package="com.bighuan.d_compositeKey"
	auto-import="true">

	<class name="User">
		<!-- 複合主鍵對映 -->
		<composite-id name="keys">
			<key-property name="userName" type="string"></key-property>
			<key-property name="address" type="string"></key-property>
		</composite-id>

		<property name="age" type="int"></property>

	</class>

</hibernate-mapping>
配置複合主鍵主要是通過<composite-id>來完成!

2)hibernate.cfg.xml(上一篇部落格貼上過這個檔案,多複製一遍,加深印象)

<!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>
		<!--1, 資料庫連線配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib_demo</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">abc</property>
		
		<!--2, 資料庫方法配置:告訴hibernate使用的是什麼資料庫 -->
		<!-- hibernate在執行的時候,會根據不同的方言生成符合當前資料庫語法的sql -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!--3, 其他相關配置:3.1展示hibernate在執行時執行的sql語句-->
		<property name="hibernate.show_sql">true</property>
		
		<!-- 3.2格式化sql語句配置 -->
		<property name="hibernate.format_sql">true</property>

               <!-- 3.3自動建表 ,先刪除後建立,不管有沒有表-->
		<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->
		
		<!-- 3.4有表就不建立,反之就建立 -->
		<property name="hibernate.hbm2ddl.auto">update</property> 
		
		<!--對映配置:  載入所有對映 -->
		<mapping resource="com/bighuan/d_compositeKey/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

四,測試

1)插入資料,查詢資料

package com.bighuan.d_compositeKey;

import java.util.Date;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App2 {

	private static SessionFactory sf = null;
	static {

		sf = new Configuration().configure().buildSessionFactory();
	}

	@Test
	public void testSave() throws Exception {
		// 建立物件
		CompositeKeys keys = new CompositeKeys();
		keys.setUserName("bighuan");
		keys.setAddress("江西南昌2");
		User user = new User();
		user.setAge(25);
		user.setKeys(keys);

		// 建立session(代表一個與資料庫連線的會話)
		org.hibernate.Session session = sf.openSession();
		// 4,開啟事務
		Transaction tx = session.beginTransaction();
		// 5,儲存資料
		session.save(user);

		tx.commit();
		// 7,關閉
		session.close();
	}

	@Test
	public void testQuery() throws Exception {
		// 建立物件
		CompositeKeys keys = new CompositeKeys();
		keys.setUserName("bighuan");
		keys.setAddress("江西南昌");

		// 建立session(代表一個與資料庫連線的會話)
		org.hibernate.Session session = sf.openSession();
		// 4,開啟事務
		Transaction tx = session.beginTransaction();
		// 5,查詢資料
		User user = (User) session.get(User.class, keys);

		if (user != null) {
			System.out.println(user.getAge() + ","
					+ user.getKeys().getAddress() + ","
					+ user.getKeys().getUserName());
		}

		tx.commit();
		// 7,關閉
		session.close();
	}

}
2)插入兩條資料到資料庫,資料庫的user表

3)通過主鍵查詢

25,江西南昌,bighuan