Hibernate联合主键(composite-id)

2010-11-02 22:16 4306人阅读 评论(0) 收藏 举报hibernateclassstringsessiondatabasejdbc

Student.java

package com.model;

 

 public class Student {

private PKStudent pk;

private String sex;

public PKStudent getPk() {

    return pk;

}

public void setPk(PKStudent pk) {

    this.pk = pk;

}

public String getSex() {

    return sex;

}

public void setSex(String sex) {

    this.sex = sex;

}

 

 

}

PKStudent.java

package com.model;

 

import java.io.Serializable;

 

public class PKStudent implements Serializable{

private int id;

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;

}

private String name;

}

StudentTest.java

package com.model;

 

 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

//import org.hibernate.cfg.Configuration;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Test;

 

public class StudentTest {

    private static SessionFactory sf=null;

@BeforeClass

public static void beforeClass()

{

     try {

       sf=(new Configuration()).configure().buildSessionFactory();

    } catch (HibernateException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

    }

}

@Test

public void testStudentSave()

{   PKStudent pk=new PKStudent();

    pk.setId(1);

    pk.setName("南山");

    Student s=new Student();

    s.setPk(pk);

    s.setSex("");

   

    Session session=sf.openSession();

    session.beginTransaction();

    session.save(s);

    session.getTransaction().commit();

    session.close();

//  sf.close();

}

@AfterClass

public static void afterClass()

{

    sf.close();

}

 

}

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 package="com.model">

<class name="Student" table="_student">

       <composite-id name="pk">

        <key-property name="id"/>

        <key-property name="name"/>

</composite-id>

       

        <property name="sex"/>

    </class>

 

</hibernate-mapping>

注意:<composite-id>name=”pk”一定要写,否则会报错;也不能写成其他的值,因为在Student.java中设置联合主键为pk<class>中的name也不能写成student

参考文档:<class>name (optional): the fully qualified Java class name of the persistent class or interface. If this attribute is missing, it is assumed that the mapping is for a non-POJO entity.

由此可见,name是作为类名的。

 

hibernate.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">

 

<hibernate-configuration>

 

    <session-factory>

 

        <!-- Database connection settings -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>

        <property name="connection.username">root</property>

        <property name="connection.password">huangjing</property>

 

        <!-- JDBC connection pool (use the built-in) -->

        <property name="connection.pool_size">1</property>

 

        <!-- SQL dialect -->

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

 

        <!-- Enable Hibernate's automatic session context management -->

        <property name="current_session_context_class">thread</property>

 

        <!-- Disable the second-level cache  -->

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

 

        <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

 

        <!-- Drop and re-create the database schema on startup -->

        <property name="hbm2ddl.auto">update</property>

 

        <mapping resource="com/model/Student.hbm.xml"/>

 

    </session-factory>

 

</hibernate-configuration>

注意:联合主键中作为主键类,一定要实现serializable类。