Hibernate学习笔记(三)----ID生成策略、联合主键

一、ID生成策略(参考hibernate_0400_ID)

1)xml生成id使用generator属性

        

             native">

        

常用四个:native identitysequence uuid

2)注解方式:@GeneratedValue

a)           自定义ID

b)       AUTO(直接写 @GeneratedValue 相当如native) (@GeneratedValue(strategy=GenerationType.AUTO))

                      i.     默认:对 MySQL,使用auto_increment

                   ii.     对 Oracle使用hibernate_sequence(名称固定)

c)       IDENTITY(@GeneratedValue(strategy=GenerationType.IDENTITY))

d)       SEQUENCE(@GeneratedValue(strategy=GenerationType.SEQUENCE))

                      i.     @SequenceGenerator(可自定义在数据库生成指定的sequence名)

          @Id

//@GeneratedValue中增加generator="teacherSEQ"

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="teacherSEQ")

public int getId() {

                    returnid;

}

//"teacherSEQ"@SequenceGenerator的标识名

//"teacherSEQ_DB"为指定到数据库生成的Sequence

@Entity

@SequenceGenerator(name="teacherSEQ",sequenceName="teacherSEQ_DB")

e)       TABLE (可以忘记)

                        i.       @TableGenerator

@TableGenerator(

name="teacherID",                 //被调用的TABLE名字

            table="teacherID_DB",                //数据库建立的表名

            pkColumnName="key_value",

            pkColumnValue="pk_value",

            valueColumnName="teacher",             //pkColumnValue对应类名

            allocationSize=1                          //pkColumnValue对应类名

)

 

@GeneratedValue(strategy=GenerationType.TABLE,generator="teacherID ")

 

       注:如果使用注解方式的uuid 如下:

              @Id

@GeneratedValue(generator="teacherUUID")

@GenericGenerator(name="teacherUUID",strategy="uuid")

 

二、联合主键

a)  Xml方式: composite-id

                     i.       将联合主键的属性提取出来,重新编写一个pojo类(原pojo类中的id,name要删除并新加入属性“StudentPK”)

       public class StudentPK implements Serializable {

                   privateString id;

                   privateString name;

                   … …

                 ii.       新建pojo类必须实现 java.io.Serializable 序列化接

              iii.       新pojo类要重写equals和hashCode方法

@Override

public boolean equals(Object o) {

       if(oinstanceof StudentPk) {

              StudentPkpk = (StudentPk)o;

              if(this.id== pk.getId() && this.name.equals(pk.getName())) {

                return true;

              }

       }

       returnfalse;

}

 

@Override

public int hashCode() {

       returnthis.name.hashCode();

}

                 iv.       联合主键生成策略XML配置方法

      

                  

                           

                                    

                                    

                           

                           

                           

                           

                  

 

b)  Annotation

                     i.       前三步与Xml方式前三步一样 都要建立新pojo类 都要实现Serializable接口 重写equals和hashCode方法.

                 ii.       方法1在新类前写@Embeddable,在原pojo类的新属性“TercherPK”的get方法前写@ld,如下

      @ Embeddable

public class TeacherPK implements Serializable {

                   privateString id;

                   privateString name;

                   … …

 

            @Entity

public classTeacher {

privateTeacherPK teacherPK ;

@Id

publicTeacherPK getTeacherPK() {

                            returnteacherPK;

}

… …

              iii.       方法2:@EmbeddedlD(*)  新pojo类无需加注解,只需在原pojo类新属性“TercherPK”的get方法前写@EmbeddedlD即可

                    iv.       方法3:@Id @IdClass(*)  新pojo类无需加注解,原pojo类的id,name属性保留不变,也无需新增“TercherPK”属性。 只在id,name的get方法前都加@Id,并在原pojo类前加@IdClass(TeacherPK).class),如下

@Entity

@IdClass(TeacherPK.class)

public class Teacher {

private String id;

private String name;

@Id

public String getId() {

              return id;

}

@Id

public String getName() {

              return name;

}

 


你可能感兴趣的:(Hibernate)