1.在很多应用中经常会有复合主键出现的情况。比如说 成绩表,需要学生编号和课程编号组成一个复合主键。
2. 类的结构: Result.java
/** * 复合主键 必须实现Serializable接口 */ public class Result implements Serializable{ private int stuId; private int subId; private double score; private Date examDate; public Result() { } public Result(int stuId, int subId, double score, Date examDate) { super(); this.stuId = stuId; this.subId = subId; this.score = score; this.examDate = examDate; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public int getSubId() { return subId; } public void setSubId(int subId) { this.subId = subId; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public Date getExamDate() { return examDate; } public void setExamDate(Date examDate) { this.examDate = examDate; } }
3. 映射文件:
<hibernate-mapping> <class name="cn.wh.vo.Result" table="t_result"> <composite-id> <key-property name="stuId"/> <key-property name="subId"/> </composite-id> <property name="score"/> <property name="examDate"/> </class> </hibernate-mapping>
4. 测试:
public class HibernateTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数 是否将脚本导入数据库执行 export.create(true, true); } @Test public void testInit(){ Transaction tx = null; try { tx = session.beginTransaction(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Result r = new Result(1, 1, 90, format.parse("2016-09-12")); session.save(r); tx.commit(); } catch (Exception e) { if(tx!=null) tx.rollback(); } } @Test public void testGet(){ Result r = new Result(); r.setStuId(1); r.setSubId(1); r = (Result)session.get(Result.class, r); System.out.println(r.getScore()+"---"+r.getExamDate()); } }
5. 复合主键的第二类结构及写法: ResultPK.java
/** * 建立一个主键类 由该类来实现序列化接口 * @author siggy * */ public class ResultPk implements Serializable{ private int stuId; private int subId; public ResultPk() { // TODO Auto-generated constructor stub } public ResultPk(int stuId, int subId) { super(); this.stuId = stuId; this.subId = subId; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public int getSubId() { return subId; } public void setSubId(int subId) { this.subId = subId; } }
Result.java
public class Result{ private ResultPk pk; private double score; private Date examDate; public Result() { } public Result(ResultPk pk, double score, Date examDate) { super(); this.pk = pk; this.score = score; this.examDate = examDate; } public ResultPk getPk() { return pk; } public void setPk(ResultPk pk) { this.pk = pk; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public Date getExamDate() { return examDate; } public void setExamDate(Date examDate) { this.examDate = examDate; } }
6. 映射文件
<hibernate-mapping> <class name="cn..vo.Result" table="t_result"> <composite-id name="pk" class="cn.wh.vo.ResultPk"> <key-property name="stuId"/> <key-property name="subId"/> </composite-id> <property name="score"/> <property name="examDate"/> </class> </hibernate-mapping>
7. 测试:
public class HibernateTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数 是否将脚本导入数据库执行 export.create(true, true); } @Test public void testInit(){ Transaction tx = null; try { tx = session.beginTransaction(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ResultPk pk = new ResultPk(1, 1); Result r = new Result(pk,90, format.parse("2016-09-12")); session.save(r); tx.commit(); } catch (Exception e) { if(tx!=null) tx.rollback(); } } @Test public void testGet(){ ResultPk pk = new ResultPk(1, 1); Result r = (Result)session.get(Result.class, pk); System.out.println(r.getScore()+"---"+r.getExamDate()); } }