hibernate映射文件中的id标签以及联合主键
来源:互联网 发布:西安软件新城二期工程 编辑:程序博客网 时间:2024/05/17 22:31
一、id标签
被映射的类必须定义对应数据库表主键字段。大多数类有一个JavaBeans风格的属性, 为每一个实例包含唯一的标识。<id> 元素定义了该属性到数据库表主键字段的映射
二、联合场景
学生表Student是由id和name联合形成主键
三、联合主键类的编写
1)实现java.io.Serializable接口
2)必须重写equals() 和 hashCode() 方法
Java代码
- package com.linys.model;
- /**
- * StudentId entity. @author MyEclipse Persistence Tools
- */
- public class StudentId implements java.io.Serializable {
- // Fields
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- // Constructors
- /** default constructor */
- public StudentId() {
- }
- /** full constructor */
- public StudentId(Integer id, String name) {
- this.id = id;
- this.name = name;
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public boolean equals(Object other) {
- if ((this == other))
- return true;
- if ((other == null))
- return false;
- if (!(other instanceof StudentId))
- return false;
- StudentId castOther = (StudentId) other;
- return ((this.getId() == castOther.getId()) || (this.getId() != null
- && castOther.getId() != null && this.getId().equals(
- castOther.getId())))
- && ((this.getName() == castOther.getName()) || (this.getName() != null
- && castOther.getName() != null && this.getName()
- .equals(castOther.getName())));
- }
- public int hashCode() {
- int result = 17;
- result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
- result = 37 * result
- + (getName() == null ? 0 : this.getName().hashCode());
- return result;
- }
- }
三、学生类的编写
Java代码
- package com.linys.model;
- /**
- * Student entity. @author MyEclipse Persistence Tools
- */
- public class Student implements java.io.Serializable {
- // Fields
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private StudentId id;
- private Integer age;
- // Constructors
- /** default constructor */
- public Student() {
- }
- /** minimal constructor */
- public Student(StudentId id) {
- this.id = id;
- }
- /** full constructor */
- public Student(StudentId id, Integer age) {
- this.id = id;
- this.age = age;
- }
- // Property accessors
- public StudentId getId() {
- return this.id;
- }
- public void setId(StudentId id) {
- this.id = id;
- }
- public Integer getAge() {
- return this.age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
四、映射文件
Student.hbm.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.linys.model.Student" table="student">
- <composite-id name="id" class="com.linys.model.StudentId">
- <key-property name="id" type="java.lang.Integer">
- <column name="id" />
- </key-property>
- <key-property name="name" type="java.lang.String">
- <column name="name" length="50" />
- </key-property>
- </composite-id>
- <property name="age" type="java.lang.Integer">
- <column name="age" />
- </property>
- </class>
- </hibernate-mapping>
其中:
<composite-id 指定联合主键的信息
<key-property 指定在StudentId类中的联合信息
五、测试类
Java代码
- package com.linys.model;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class TestUniPk {
- static SessionFactory sf;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- sf = new Configuration().configure().buildSessionFactory();
- }
- @Test
- public void testUniPk(){
- StudentId stuId=new StudentId();
- //stuId.setId(1);
- stuId.setName("linys");
- Student stu=new Student();
- stu.setAge(23);
- stu.setId(stuId);
- Session session =sf.openSession();
- Transaction tr=session.beginTransaction();
- session.save(stu);
- tr.commit();
- session.save(stu);
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- sf.close();
- }
- }
0 0
- hibernate映射文件中的id标签以及联合主键
- Hibernate联合主键映射以及组件映射
- hibernate里联合主键composite-id映射
- Hibernate映射联合主键
- Hibernate联合主键映射
- hibernate映射联合主键
- Hibernate联合主键映射
- Hibernate联合主键映射
- Hibernate联合主键映射
- Hibernate联合主键映射
- hibernate联合主键映射
- Hibernate联合主键映射
- Hibernate联合主键映射
- hibernate映射主键 id
- hibernate 联合主键 composite-id
- hibernate ID联合生成主键
- hibernate ID联合生成主键
- hibernate 联合主键 composite-id
- Python-OpenCV 处理视频(五): 运动方向判断
- Android内存优化杂谈
- h5案例分享:王中军个人画展:藝術東西,玄如藝術
- Hive问题:HiveConf of name hive.metastore.local does not exist
- 【整理】Python中:self和__init__的含义 + 为何要有self和__init__
- hibernate映射文件中的id标签以及联合主键
- 添加数据文件,扩充表空间
- mysql操作命令记录
- 奥园地产转型:对外连接客户 对内连接员工
- 《Java编程思想》学习笔记19——并发编程(二)
- ORA-27101: shared memory realm does not exist
- 数据解析为json格式
- Python-OpenCV 处理图像(六):对象识别
- oracle merge into的用法