hibernate复合主键方式(2)
来源:互联网 发布:淘宝海运价格表 编辑:程序博客网 时间:2024/06/05 08:21
方式二:将主键属性提取到一个主键类中,实体类只需包含主键类的一个引用。
主键类:
/*必须实现Serializable接口*/
public class PeoplePrimaryKey implements Serializable
{
private static final long serialVersionUID = -1190986010439330142L;
/*复合主键值*/
private String id;
private String name;
public PeoplePrimaryKey()
{
}
/*复合主键值的get和set方法*/
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PeoplePrimaryKey other = (PeoplePrimaryKey) obj;
if (id == null)
{
if (other.id != null)
return false;
}
else if (!id.equals(other.id))
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
}
实体类:
public class People {
/*持有主键类的一个引用,使用该引用作为这个类的OID*/
private PeoplePrimaryKey peoplePrimaryKey;
private int age;
public People()
{
}
public PeoplePrimaryKey getPeoplePrimaryKey()
{
return peoplePrimaryKey;
}
public void setPeoplePrimaryKey(PeoplePrimaryKey peoplePrimaryKey)
{
this.peoplePrimaryKey = peoplePrimaryKey;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
People.hbm.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">
<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.People" table="people">
<!-- 复合主键使用composite-id标签 -->
<!-- name - 指定了复合主键对应哪一个属性
class - 指定了复合主键属性的类型 -->
<composite-id name="peoplePrimaryKey" class="com.suxiaolei.hibernate.pojos.PeoplePrimaryKey">
<!-- key-property指定了复合主键由哪些属性组成 -->
<key-property name="id" column="id" type="string"></key-property>
<key-property name="name" column="name" type="string"></key-property>
</composite-id>
<property name="age" column="age" type="integer"></property>
</class> </hibernate-mapping>
场景测试与方式一大同小异这里不再举例了。主键类为什么实现Serializable接口和为什么重写equals和hashCode方法上面已经解释的很清楚了。
- hibernate复合主键方式(2)
- hibernate复合主键的两种方式(1)
- hibernate注解方式实现复合主键
- hibernate注解方式实现复合主键
- hibernate注解方式实现复合主键
- hibernate注解方式实现复合主键
- Hibernate(十一)hibernate复合主键映射
- Hibernate中的复合主键映射(了解)
- Hibernate组件作为复合主键(五)
- Hibernate复合主键
- hibernate 复合主键
- hibernate复合主键
- Hibernate复合主键例子
- Hibernate复合主键
- Hibernate复合主键
- Hibernate 复合主键
- hibernate中复合主键
- hibernate复合主键映射
- InstallShield内部库函数全集(中文版)
- 元素之间的位置关系
- windows ubuntu 双系统 蓝屏Technical information: ***stop:0x0000007B(0x80786B58,0xC0000034,0x00000000,0x00
- 关于华为一道面试题 ( ++a )和( a++ )
- Tomcat60 换 Tomcat7.0 项目启动异常IllegalArgumentException: taglib definition not consistent with specific
- hibernate复合主键方式(2)
- hdu(4007)正方形能框住最大的点数
- mysql乱码解决方法 无法插入 控制台乱码
- 由中序和后续序列建立二叉树
- <<探讨linux/unix>>之df
- paypal账户防风险实用3招小技巧
- SSH通信协议浅析
- 《编程之美》和《无以言退》
- make menuconfig出错