联合主键配置

发布时间:2016-12-6 13:54:52 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"联合主键配置",主要涉及到联合主键配置方面的内容,对于联合主键配置感兴趣的同学可以参考一下。

联合主键的配置很少用,也很简单,主要是配置文件里id的配置.在load的时候传递的是设置主键后的对象,对象必须是序列化的. 1,实体Employee.java package com.model; import java.io.Serializable; public class Employee implements Serializable{ private String empNo; private String empName; private String address; private int age; public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 2,Employee.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.model"> <class name="Employee" table="employee"> <composite-id> <key-property name="empNo"></key-property> <key-property name="empName"></key-property> </composite-id> <property name="address" column="address"></property> <property name="age" column="age"></property> </class> </hibernate-mapping>3,单元测试 @Test public void testEmployee(){ Session s = HibernateUtil.getInstance().getSession(); s.beginTransaction(); //添加员工 Employee e = new Employee(); e.setEmpNo("01"); e.setEmpName("name01"); e.setAddress("sdfdf"); e.setAge(20); s.save(e); //查询员工 Employee e2 = new Employee(); e2.setEmpNo("01"); e2.setEmpName("name01"); Employee employee = (Employee) s.load(Employee.class, e2); System.out.println(employee.getAddress()); s.getTransaction().commit(); s.close(); } 4,输出 sdfdf Hibernate: insert into employee (address, age, empNo, empName) values (?, ?, ?, ?)

上一篇:VC++ 获取临时目录
下一篇:Iterator 迭代 警惕 java.util.ConcurrentModificationException异常

相关文章

关键词: 联合主键配置

相关评论