欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3.hibernate联合主键,xml映射

程序员文章站 2022-04-25 08:29:40
...

package com.bjsxt.hibernate;

public class StudentPK implements java.io.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 o) {
if(o instanceof StudentPK) {
StudentPK pk = (StudentPK)o;
if(this.id == pk.getId() && this.name.equals(pk.getName())) {
return true;
}
}
return false;
}

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




package com.bjsxt.hibernate;

public class Student {

private StudentPK pk;

private int age;
private String sex;
private boolean good;
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
/*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;
}*/
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public StudentPK getPk() {
return pk;
}
public void setPk(StudentPK pk) {
this.pk = pk;
}


}




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


<composite-id name="pk" class="com.bjsxt.hibernate.StudentPK">
<key-property name="id"></key-property>
<key-property name="name"></key-property>
</composite-id>

<property name="age" />
<property name="sex" />
<property name="good" type="yes_no"></property>
</class>

</hibernate-mapping>