如何数据库表只有一个主键,我们可以这样配置
1 | <hibernate-mapping package = "com.study.model" > |
2 | < class name= "Student" table= "student12" > |
3 | <id name= "id" column= "id" > |
4 | <generator class = "native" ></generator> |
6 | <property name= "name" column= "name" type= "java.lang.String" ></property> |
7 | <property name= "age" column= "age" type= "java.lang.Integer" ></property> |
或者我们用annotation:一般将@id写在get方法上,其实也可卸载field字段上,但是那样的话会破坏pojo原则
2 | @GeneratedValue (strategy=GenerationType.IDENTITY) |
但是如何我们的数据库表有两个主键该怎么办呢?答案是:compose key联合主键
注意:(1)我们必须用另一个类作为我们的主键类,在第一个pojo中调用这个主键类
(2)在主键类中,我们必须重写他的equals()和hashcode()方法!
理由:我们的类都间接或者直接集成自object类,而在object中的equals和hashcode方法如下:
hashcode介绍: http://blog.csdn.net/jason_deng/article/details/7074230
1 | public boolean equals(Object obj) { |
5 | public native int hashCode(); |
既然equals方法只是比较两个类是否“==”,那么,对于主键类就是比较每个字段是否相等。
hashcode主要是表征对象的唯一性
(3)主键类还必须实现serializable接口,主要考虑到类的在网络传递,在不同存储介质上转存
下面我们就来写我们自己的联合主键:
1、主键pojo类:
01 | public class composeIdPK implements Serializable { |
04 | public String getName() { |
07 | public void setName(String name) { |
13 | public void setId( int id) { |
17 | public boolean equals(Object o) { |
18 | composeIdPK pk = null ; |
19 | if (o instanceof composeIdPK) |
21 | if (pk.getId() == this .id && pk.getName().equals( this .name)) |
29 | return this .name.hashCode(); |
pojo类
01 | package com.study.model; |
02 | public class composeId { |
05 | private composeIdPK pk; |
08 | private String address; |
13 | public void setUid( int uid) { |
16 | public composeIdPK getPk() { |
19 | public void setPk(composeIdPK pk) { |
22 | public String getTitle() { |
25 | public void setTitle(String title) { |
28 | public String getAddress() { |
31 | public void setAddress(String address) { |
32 | this .address = address; |
配置文件:
01 | <hibernate-mapping package = "com.study.model" > |
02 | < class name= "composeId" table= "composeIDTest" > |
04 | <composite-id name= "pk" class = "com.study.model.composeIdPK" > |
05 | <key-property name= "id" ></key-property> |
06 | <key-property name= "name" ></key-property> |
08 | <property name= "title" column= "title" type= "java.lang.String" ></property> |
09 | <property name= "address" column= "address" type= "java.lang.String" ></property> |
说明:
节点(composite-id)表明是一个联合主键,但是在<class name="composeId" table="composeIDTest">
的束缚下,表明是com.study.model.composeId下的联合主键,并且name=pk必须是和composeId下一致,因为在调用的时候会直接用pk的getter方法调用composeIdPK类从而取得该pojo类的主键
测试类:
04 | composeIdPK pk= new composeIdPK(); |
06 | pk.setName( "jackvin" ); |
07 | composeId compose= new composeId(); |
08 | compose.setTitle( "hah" ); |
09 | compose.setAddress( "sffds" ); |
14 | Configuration cfg= new AnnotationConfiguration(); |
18 | SessionFactory factory=cfg.buildSessionFactory(); |
20 | Session session=factory.openSession(); |
21 | session.beginTransaction(); |
22 | session.save(compose); |
23 | session.getTransaction().commit(); |
27 | } catch (Exception e) { |
34 | fail( "Not yet implemented" ); |
ok测试通过
转:http://my.oschina.net/u/942629/blog/190596