基于XML的复合主键

<?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 package="com.hibernate.demo.po">
    <class name="Teacher" table="teacher">
        <composite-id name="pk" class="TeacherPK">
        	<key-property name="id" type="string"/>
        	<key-property name="name" type="string"/>
        </composite-id>
    </class>
</hibernate-mapping>

 TeacherPK.java

package com.hibernate.demo.po;

import java.io.Serializable;

public class TeacherPK implements Serializable {
	
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		TeacherPK pk = (TeacherPK)obj;
		if(this.id==pk.getId() && this.name.equals(pk.getName())){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int hashCode() {
		return this.name.hashCode();
	}
	
}

 

 

你可能感兴趣的:(基于XML的复合主键)