需求: 在T_STUDENTS表中 要将ID和NAME联合作为主键:
1.将id和name属性定义到一个主键类中:StudentPK 并重写hashCode()和equals()方法同时要继承Serializable接口
package com.zchen.hibernate.sxt.domain;
import java.io.Serializable;
public class StudentPK implements Serializable{
/**
*
*/
private static final long serialVersionUID = -7880746333829652369L;
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();
}*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
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;
StudentPK other = (StudentPK) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
2.定义实体类: Students 将主键类放到里面:
package com.zchen.hibernate.sxt.domain;
public class Students {
private StudentPK pk;
public StudentPK getPk() {
return pk;
}
public void setPk(StudentPK pk) {
this.pk = pk;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private int age;
private String address;
}
3.定义Students.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 package="com.zchen.hibernate.sxt.domain">
<class name="Students" table="T_STUDENTS">
<composite-id name="pk" class="com.zchen.hibernate.sxt.domain.StudentPK">
<key-property name="id" column="ID"/>
<key-property name="name" column="NAME"/>
</composite-id>
<property name="age" column="AGE"/>
<property name="address" column="ADDRESS"/>
</class>
</hibernate-mapping>
4.定义配置文件:Hibernate.cfg.xml 略
<?xml version="1.0"?>
<!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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="com/zchen/hibernate/sxt/domain/Students.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.测试用例:
package com.zchen.hibernate.sxt.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.zchen.hibernate.sxt.domain.StudentPK;
import com.zchen.hibernate.sxt.domain.Students;
public class StudentsTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
try {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
@Test
public void addStduentTest(){
StudentPK pk = new StudentPK();
pk.setId(1);
pk.setName("zchen");
Students s = new Students();
s.setAge(24);
s.setAddress("北京");
s.setPk(pk);
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}
@AfterClass
public static void afterClass(){
if(sf != null){
try {
sf.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
}
输出语句:
11:32:09,818 INFO SchemaExport:226 - Running hbm2ddl schema export
11:32:09,821 DEBUG SchemaExport:242 - import file not found: /import.sql
11:32:09,821 INFO SchemaExport:251 - exporting generated schema to database
11:32:09,822 DEBUG SchemaExport:377 - drop table if exists T_STUDENTS
11:32:09,879 DEBUG SchemaExport:377 - create table T_STUDENTS (ID integer not null, NAME varchar(255) not null, AGE integer, ADDRESS varchar(255), primary key (ID, NAME))
11:32:09,953 INFO SchemaExport:268 - schema export complete
Hibernate: insert into T_STUDENTS (AGE, ADDRESS, ID, NAME) values (?, ?, ?, ?)
本文介绍如何在Hibernate框架中实现复合主键,包括创建复合主键类、实体类及映射文件等内容,并通过一个具体的例子展示了整个过程。
1184

被折叠的 条评论
为什么被折叠?



