首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Hibernate联结主键生成策略

2013-08-29 
Hibernate联合主键生成策略package com.test.hiberenate.modelimport java.io.Serializablepublic class

Hibernate联合主键生成策略
package com.test.hiberenate.model;import java.io.Serializable;public class StudentPK implements Serializable {private int id;private String name;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;}public boolean equals(Object o) {if (o instanceof StudentPK) {StudentPK pk = (StudentPK) o;if (this.id == pk.getId() && this.name.equals(pk.getName())) {return true;}}return false;}public int hashCode() {return this.name.hashCode();}}

package com.test.hiberenate.model;public class Student {private StudentPK pk;private int age;private boolean good;public StudentPK getPk() {return pk;}public void setPk(StudentPK pk) {this.pk = pk;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isGood() {return good;}public void setGood(boolean good) {this.good = good;}}

<?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.test.hiberenate.model"><class name="Student"><composite-id name="pk" /><property name="good" /></class></hibernate-mapping>

package com.test.hiberenate.model;import java.io.Serializable;public class TeacherPK implements Serializable {private int id;private String name;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;}public boolean equals(Object o) {if (o instanceof TeacherPK) {TeacherPK pk = (TeacherPK) o;if (this.id == pk.getId() && this.name.equals(pk.getName())) {return true;}}return false;}public int hashCode() {return this.name.hashCode();}}

package com.test.hiberenate.model;import java.util.Date;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.IdClass;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;/** * 定义组合主键的三种语法: * 1.将组件类注解为@Embeddable,并将实体类中组件的属性注解为@Id * 2.将实体类中组件的属性注解为@EmbeddedId * 3.将实体类注解为@IdClass,并将该实体类中所有属于主键的属性都注解为@Id */@Entity@IdClass(TeacherPK.class)public class Teacher {private int id;private String name;private String title;private String description;private Date birthday;@Idpublic int getId() {return id;}public void setId(int id) {this.id = id;}@Idpublic String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@Transientpublic String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@Temporal(TemporalType.DATE)public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}

为了保证对象唯一性的。多个对象放在内存中,他们之间用什么区分?重写组件类的hashCode()和equal()方法来区分,数据库中是用那个联合主键来区分的。

热点排行