类中的每个主键属性都对应到数据库表中的每个主键列,hibernate要求具有联合主键
的实体类实现Serializable接口,并且重写hashCode和equals方法,重写这两个方法是因为
hibernate要根据数据库的联合主键来判断某两行记录是否是一样的,如果一样那么就认为
是同一个对象,如果不一样,那么认为是不同的对象,这反映到程序领域中就是根据hashCode
与equals方法来判断某两个对象是否能够放到诸如Set这样的集合当中
联合主键的实体类实现Serializable接口的原因在于使用get或load方法的时候需要先构建出
来该实体的对象,并且将查询依据(联合主键)设置进去,然后作为get或load方法第二个参数
传进去即可
场景:Student类中有cardId和name两个属性作为主键,现在是这两个属性都在Student这一个类里面,没有分离出来,然后配置文件中映射的时候直接映射这个类里面的这两个属性为主键
Student.java
package com.fgh.hibernate;
import java.io.Serializable;
/**
* 学生类<br>
* name和cardId为联合主键
*
* @author fgh
*
*/
public class Student implements Serializable {
private static final long serialVersionUID = 2574682166458317992L;
private String name;
private String cardId;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cardId == null) ? 0 : cardId.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Student other = (Student) obj;
if (cardId == null) {
if (other.cardId != null) {
return false;
}
} else if (!cardId.equals(other.cardId)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
} else if (!name.equals(other.name)) {
return false;
}
}
return true;
}
}
Student.hbm.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>
<class name="com.fgh.hibernate.Student" table="student_composite">
<!-- 主键映射 -->
<composite-id>
<key-property name="name" column="name" type="string"></key-property>
<key-property name="cardId" column="cardId" type="string"></key-property>
</composite-id>
<property name="age" column="age" type="int"></property>
</class>
</hibernate-mapping>
hinernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
建表类:CreateTable.java
package com.fgh.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, true);
}
}
测试类:hibernateTest.java
package com.fgh.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* 联合主键一
*
* @author fgh
*
*/
public class hibernateTest {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// 保存
// Student student = new Student();
// student.setCardId("123");
// student.setName("zhangsan");
// student.setAge(20);
// 查询
Student studentPrimaryKey = new Student();
studentPrimaryKey.setCardId("123");
studentPrimaryKey.setName("zhangsan");
Student student = (Student) session.get(Student.class,
studentPrimaryKey);
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(studentPrimaryKey.getAge());
// session.save(student);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (null != tx) {
tx.rollback();
}
} finally {
session.close();
}
}
}
Thanks,Over!
I hope for your
help
!