hibernate学习11之composite(复合)主键映射

复合主键映射可以看作是组件映射的一个特例.

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_composite_mapping</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">bjsxt</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		
		<mapping resource="com/bjsxt/hibernate/FiscalYearPeriod.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法
import java.io.Serializable;

public class FiscalYearPeriodPK implements Serializable {

	//核算年
	private int fiscalYear;
	
	//核算月
	private int fiscalPeriod;

	public int getFiscalYear() {
		return fiscalYear;
	}

	public void setFiscalYear(int fiscalYear) {
		this.fiscalYear = fiscalYear;
	}

	public int getFiscalPeriod() {
		return fiscalPeriod;
	}

	public void setFiscalPeriod(int fiscalPeriod) {
		this.fiscalPeriod = fiscalPeriod;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + fiscalPeriod;
		result = prime * result + fiscalYear;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
		if (fiscalPeriod != other.fiscalPeriod)
			return false;
		if (fiscalYear != other.fiscalYear)
			return false;
		return true;
	}
	
}

public class FiscalYearPeriod {
//复合主键
	private FiscalYearPeriodPK fiscalYearPeriodPK;
	//开始日期
	private Date beginDate;
	//结束日期
	private Date endDate;
	//状态
	private String periodSts;
	//setter,getter
}

复合主键映射的配置:
<?xml version="1.0"?>
<!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.bjsxt.hibernate.FiscalYearPeriod" table="t_fiscal_year_period">
		<composite-id name="fiscalYearPeriodPK">
			<key-property name="fiscalYear"/>
			<key-property name="fiscalPeriod"/>	
		</composite-id>
		<property name="beginDate"/>
		<property name="endDate"/>
		<property name="periodSts"/>
	</class>
</hibernate-mapping>

测试一下:
import java.util.Date;

import org.hibernate.Session;

import junit.framework.TestCase;

public class CompositeMappingTest extends TestCase {

	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			FiscalYearPeriod fiscalYearPeriod = new FiscalYearPeriod();

			FiscalYearPeriodPK pk = new FiscalYearPeriodPK();
			pk.setFiscalYear(2008);
			pk.setFiscalPeriod(8);
			
			fiscalYearPeriod.setFiscalYearPeriodPK(pk);
			
			fiscalYearPeriod.setBeginDate(new Date());
			fiscalYearPeriod.setEndDate(new Date());
			fiscalYearPeriod.setPeriodSts("Y");
			
			session.save(fiscalYearPeriod);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			FiscalYearPeriodPK pk = new FiscalYearPeriodPK();
			pk.setFiscalYear(2008);
			pk.setFiscalPeriod(8);
			
			FiscalYearPeriod fiscalYearPeriod = (FiscalYearPeriod)session.load(FiscalYearPeriod.class, pk);
			
			System.out.println(fiscalYearPeriod.getPeriodSts());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}			
}

你可能感兴趣的:(Hibernate,.net,mysql,jdbc,JUnit)