方法一:主键类用@Embeddable,pojo类仍然用@Entity但是引用主键类的对象用@Id
主键pojo类:
02 | public class composeIdPK implements Serializable { |
05 | @Column (length= 20 ,name= "pkName" ) |
06 | public String getName() { |
09 | @Column (length= 10 ,name= "uuid" ) |
pojo类:
02 | public class composeId { |
03 | private composeIdPK pk; |
06 | private String address; |
09 | public composeIdPK getPk() { |
方法二:@EmbeddedlD(*) 主键pojo类无需加@EmbeddedlD注解,只需在pojo类新属性“composeIdPK”的get方法前写@EmbeddedlD即可
方法三:@Id @IdClass(*) 主键pojo类无需加注解,原pojo类的id,name属性保留不变,也无需新增“ComposeIDPK”属性。 只在id,name的get方法前都加@Id,并在原pojo类前加
如下:
02 | @IdClass (com.study.model.composeID.composeIdPK. class ) |
03 | public class composeId { |
08 | @Column (length= 10 ,name= "uuid" ) |
12 | public void setId( int id) { |
16 | @Column (length= 20 ,name= "pkName" ) |
17 | public String getName() { |
20 | public void setName(String name) { |
24 | private String address; |
测试ok!
转:http://my.oschina.net/u/942629/blog?catalog=431496