如果一个表有多个主键(= =一般比较少)
8.4. 组件作为联合标识符(Components as composite identifiers)
先定义一个类OrderLineId
(实现接口,implems java.io.Serializable(把当前对象序列化的接口)),
重写
为什么要序列化?内存满和集群化的时候用,具体百度(= =!其实我就是不懂)
里面有多个成员变量(字段(这些都是主键))
package com.zjx.hibernate; public class StudentPK implements java.io.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; } @Override public boolean equals(Object o) { //判断是否相等,相等就找它 if(o instanceof StudentPK) { //如果o是一个StudentPK对象 StudentPK pk = (StudentPK)o; if(this.id == pk.getId() && this.name.equals(pk.getName())) { return true; } } return false; } @Override public int hashCode() { //计算主键的hash码 if有多个对象 就直接找hash码 return this.name.hashCode(); } }
然后在 类Student中 创建OrderLineId成员变量,set,get一下,
package com.zjx.hibernate; public class Student { private StudentPK pk; private int age; private String sex; private boolean good; public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } /*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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this.pk = pk; } }
其他都一样 就是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> <class name="com.bjsxt.hibernate.Student"> <composite-id name="pk" class="com.zjx.hibernate.StudentPK"> <key-property name="id"></key-property> <key-property name="name"></key-property> </composite-id> <property name="age" /> <property name="sex" /> <property name="good" type="yes_no"></property> </class> </hibernate-mapping>
如果是使用注释?
2.2.. 映射主键属性
下面是定义组合主键的几种语法:
- 将组件类注解为@Embeddable,并将组件的属性注解为@Id(比较少用)
- 将组件的属性注解为@EmbeddedId
- 将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
对于EJB2的开发人员来说 @IdClass是很常见的, 但是对于Hibernate的用户来说就是一个崭新的用法.
组合主键类对应了一个实体类中的多个字段或属性,
而且主键类中用于定义主键的字段或属性和 实体类中对应的字段或属性在类型上必须一致.下面我们看一个例子:
@Entity @IdClass(FootballerPk.class) public class Footballer { //part of the id key @Id public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } //part of the id key @Id public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getClub() { return club; } public void setClub(String club) { this.club = club; } //appropriate equals() and hashCode() implementation } @Embeddable public class FootballerPk implements Serializable { //same name and type as in Footballer public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } //same name and type as in Footballer public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } //appropriate equals() and hashCode() implementation }
如上, @IdClass指向对应的主键类.
Hibernate支持在组合标识符中定义关联(就像使用普通的注解一样),而EJB3规范并不支持此类用法.
@Entity @AssociationOverride( name="id.channel", joinColumns = @JoinColumn(name="chan_id") ) public class TvMagazin { @EmbeddedId public TvMagazinPk id; @Temporal(TemporalType.TIME) Date time; } @Embeddable public class TvMagazinPk implements Serializable { @ManyToOne public Channel channel; public String name; @ManyToOne public Presenter presenter; }@IdClass(TeacherPK.class);
那么就能使用TeacherPK的东西;
t.setId(1);
t.setName("t1");
t.setName("t1");
版权声明:本文为博主原创文章,未经博主允许不得转载。
today lazy . tomorrow die .