Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解
            时间:2014-05-03 22:50:55  
            收藏:0  
            阅读:584
        
        
        参考:
http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/
1、系统配置:
可以通过使用 mapping的 resource,于class 属性混合配置
<mapping resource="com/rhythmk/model/product.hbm.xml" />
<mapping class="com.rhythmk.model.User"></mapping>
在创建SessionFactory 也可以通过代码完成映射关联:
| 1 2 3 4 5 | sessionFactory = newAnnotationConfiguration()                    .addPackage("test.animals") //the fully qualified package name                    .addAnnotatedClass(Flight.class)                    .addAnnotatedClass(Sky.class)                   .buildSessionFactory(); | 
2、实体:
》每一个持久化POJO类都是一个实体bean,这可以通过在类的定义中使用@Entity
》hibernate的访问类型分别为 field或property. EJ3规范要求在需要访问的元素上进行注解声明,例如,如果访问类型为 property就要在getter方法上进行注解声明, 如果访问类型为 field就要在字段上进行注解声明.应该尽量避免混合使用这两种访问类型
》实体bean中所有的非static非transient的属性都可以被持久化, 除非你将其注解为@Transient.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Entity@Table(name = "t_user")publicclassUser {    @Id    @GeneratedValue    publicInteger getUserID() {        returnuserID;    }    @Column(name = "create_time")    publicDate getCreateTime() {        returncreateTime;    }    publicvoidsetUserID(Integer userID) {        this.userID = userID;    }.......<br><br>} | 
| 1 2 3 4 5 6 7 8 9 10 11 | @Column(    name="columnName";                                (1)  可选,列名(默认值是属性名)    booleanunique() defaultfalse;                   (2)  可选,是否在该列上设置唯一约束(默认值false)    booleannullable() defaulttrue;                  (3)  可选,是否设置该列的值可以为空(默认值false)    booleaninsertable() defaulttrue;                (4)  可选,该列是否作为生成的insert语句中的一个列(默认值true)    booleanupdatable() defaulttrue;                 (5)  可选,该列是否作为生成的update语句中的一个列(默认值true)    String columnDefinition() default"";             (6)  可选: 为这个特定列覆盖SQL DDL片段 (这可能导致无法在不同数据库间移植)    String table() default"";                        (7)  可选,定义对应的表(默认为主表)    intlength() default255;                         (8)  可选,列长度(默认值255)    intprecision() default0; // decimal precision   (9)  可选,列十进制精度(decimal precision)(默认值0)    intscale() default0; // decimal scale           (10) 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值0) | 
3、映射继承关系:
- 每个类一张表(Table per class)策略: 在Hibernate中对应<union-class>元素:
- 每个类层次结构一张表(Single table per class hierarchy)策略:在Hibernate中对应<subclass>元素
-  
  123456789101112@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="planetype",discriminatorType=DiscriminatorType.STRING)@DiscriminatorValue("Plane")publicclassPlane { ... }@Entity@DiscriminatorValue("A320")publicclassA320extendsPlane { ... }
- 连接的子类(Joined subclasses)策略:在Hibernate中对应 <joined-subclass>元素
| 1 2 3 4 5 6 7 8 9 10 | @Entity@Inheritance(strategy=InheritanceType.JOINED)publicclassBoat implementsSerializable { ... }@EntitypublicclassFerry extendsBoat { ... }@Entity@PrimaryKeyJoinColumn(name="BOAT_ID")publicclassAmericaCupClass  extendsBoat { ... } | 
Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解,布布扣,bubuko.com
            评论(0)
        
        
         
        