2.由一个主键类,将对象当中的联合作为主键的属性描述成一个对象,称为主键类
【主键类的复合主键】
主键类: 1.实现序列化接口[必须]
2.要为主键类提供equas和hashcode的方法【早期版本的hibernate】
1.将组成主键的属性单独作为主键类
class StuMsgPk {
stuId//学号
courseId//课程
}
2.对象
class StuMsg{
StuMsgPk;//主键类
garde ;
}
映射文件:
<composite-id name="属性名" class="属性对应的对象">
<key-property name="xxx" column="xxxx"></key-property>
...
</composite-id>
1.O在com.jsu.hb.pojo包中提供主键类和实体类StudentPK.java和Student.java
package com.jsu.hb.pojo;
import java.io.Serializable;
public class StudentPK implements Serializable{
private Integer studentId;
private Integer courseId;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
}
Student.java
package com.jsu.hb.pojo;
import java.io.Serializable;
public class Student {
private StudentPK pk;
private Double grade;
public StudentPK getPk() {
return pk;
}
public void setPk(StudentPK pk) {
this.pk = pk;
}
public Double getGrade() {
return grade;
}
public void setGrade(Double grade) {
this.grade = grade;
}
}
2.R建表
create table g_stuInfo(
studentId integer,
courseId integer,
grade number(4,1),
primary key(studentId,courseId)
)
select * from g_stuInfo
3.提供映射文件stupkc.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.jsu.hb.pojo"> <!-- name:所操作对象的全路径 table:该对象所对应的表名 --> <class name="Student" table="g_stuInfo"> <composite-id name="pk" class="StudentPK"> <key-property name="studentId" column="studentId"></key-property> <key-property name="courseId" column="courseId"></key-property> </composite-id> <!-- 普通属性的配置,非主键属性的配置 --> <property name="grade" column="grade"></property> </class> </hibernate-mapping>
4.在hibernate.cfg.xml文件中对映射文件进行注册
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- show_sql:是否显示hibernate执行的SQL语句,默认为false --> <property name="show_sql">true</property> <!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false --> <property name="format_sql">true</property> <!-- 配置与数据库连接的参数 --> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:oracle</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <!-- 2.自身属性相关的配置 dialect:方言 hibernate根据dialect的配置进行特定数据性能方面的调优 --> <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property> <mapping resource="com/jsu/hb/pojo/stupkc.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
5.提供工具类HibernateUtil.java
package com.jsu.hb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
static{
try{
Configuration cfg = new Configuration();
cfg.configure();
sf=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session openSession(){
return sf.openSession();
}
public static Session getCurrentSession(){
Session session = tl.get();//先从储存的线程中查找
if(session==null){
session=openSession();
tl.set(session);
return session;
}
return session;
}
}
6.提供测试类TestStuPKC.java
package com.jsu.hb.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.jsu.hb.pojo.StudentInfo;
import com.jsu.hb.util.HibernateUtil;
public class TestStuPKC {
@Test
public void save(){
StudentInfo s = new StudentInfo();
s.setStudentId(2);
s.setCourseId(3);
s.setGrade(80.5);
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(s);
tx.commit();
}
}