zoukankan      html  css  js  c++  java
  • Rhythmk 学习 Hibernate 07

    参考:

      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 也可以通过代码完成映射关联:

    sessionFactory = new AnnotationConfiguration()
                        .addPackage("test.animals") //the fully qualified package name
                        .addAnnotatedClass(Flight.class)
                        .addAnnotatedClass(Sky.class)
                       .buildSessionFactory();
    

      

    2、实体:

    》每一个持久化POJO类都是一个实体bean,这可以通过在类的定义中使用@Entity

    》hibernate的访问类型分别为 fieldproperty. EJ3规范要求在需要访问的元素上进行注解声明,例如,如果访问类型为 property就要在getter方法上进行注解声明, 如果访问类型为 field就要在字段上进行注解声明.应该尽量避免混合使用这两种访问类型

    实体bean中所有的非static非transient的属性都可以被持久化, 除非你将其注解为@Transient.

          

    @Entity
    @Table(name = "t_user")
    public class User {
    
    	@Id
    	@GeneratedValue
    	public Integer getUserID() {
    		return userID;
    	}
    
    	@Column(name = "create_time")
    	public Date getCreateTime() {
    		return createTime;
    	}
    
    	public void setUserID(Integer userID) {
    		this.userID = userID;
    	}
    .......

    }

      

    @Column(
        name="columnName";                                (1)  可选,列名(默认值是属性名)
        boolean unique() default false;                   (2)  可选,是否在该列上设置唯一约束(默认值false)
        boolean nullable() default true;                  (3)  可选,是否设置该列的值可以为空(默认值false)
        boolean insertable() default true;                (4)  可选,该列是否作为生成的insert语句中的一个列(默认值true)
        boolean updatable() default true;                 (5)  可选,该列是否作为生成的update语句中的一个列(默认值true)
        String columnDefinition() default "";             (6)  可选: 为这个特定列覆盖SQL DDL片段 (这可能导致无法在不同数据库间移植)
        String table() default "";                        (7)  可选,定义对应的表(默认为主表)
        int length() default 255;                         (8)  可选,列长度(默认值255)
        int precision() default 0; // decimal precision   (9)  可选,列十进制精度(decimal precision)(默认值0)
        int scale() default 0; // decimal scale           (10) 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值0)
    

    3、映射继承关系:

    • 每个类一张表(Table per class)策略: 在Hibernate中对应<union-class>元素:
    • 每个类层次结构一张表(Single table per class hierarchy)策略:在Hibernate中对应<subclass>元素
    •  
      @Entity
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(
          name="planetype",
          discriminatorType=DiscriminatorType.STRING
      )
      @DiscriminatorValue("Plane")
      public class Plane { ... }
      
      @Entity
      @DiscriminatorValue("A320")
      public class A320 extends Plane { ... }
      

        

    • 连接的子类(Joined subclasses)策略:在Hibernate中对应 <joined-subclass>元素 
    @Entity
    @Inheritance(strategy=InheritanceType.JOINED)
    public class Boat implements Serializable { ... }
    
    @Entity
    public class Ferry extends Boat { ... }
    
    @Entity
    @PrimaryKeyJoinColumn(name="BOAT_ID")
    public class AmericaCupClass  extends Boat { ... }
    

      

  • 相关阅读:
    linux2.6.24.3下移植SD/MMC到S3C2440上的全历程
    设置装备布置了下双表示器
    Virtual Box 1.5.0 - 实用的“无缝窗口”
    oracle DB LINK 运用
    Linux下的tidy安置
    Linux效劳器装机安全疾速进阶指南(2)
    Linux下历程间通信
    Firefox 3 CSS Hack
    Linux下的搜刮东西find根基用法
    Linux效能器装机平安快速进阶指南(3)
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3705182.html
Copyright © 2011-2022 走看看