在路上

          路上有驚慌,路上有理想

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            28 Posts :: 1 Stories :: 10 Comments :: 0 Trackbacks

          1.@Entity 標(biāo)識實體
          2.@Table (name = "tableName") //指定物理表

          @Table(name="tbl_sky",
              uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}//唯一性約束
          )

          3.@Embeddable 被聲明的類可以嵌入其他實體中
          public class Address {
             private String street1;//persistent
             public String getStreet1() { return street1; }
             public void setStreet1() { this.street1 = street1; }
             private hashCode; //not persistent
          }
          @Embedded 在實體中嵌入一個類型:常用的像名字,地址之類的
          另,使用@AttributeOverrides標(biāo)識覆蓋原類中的屬性取值,因為原實體可能引用的是其他字段。
           @Embedded
              @AttributeOverrides( {
                      @AttributeOverride(name="iso2", column = @Column(name="bornIso2") ),
                      @AttributeOverride(name="name", column = @Column(name="bornCountryName") )
              } )
          Country bornIn;

          例子:
          @Entity

          class User {
            @EmbeddedId
            @AttributeOverride(name="firstName", column=@Column(name="fld_firstname")
            UserId id;
            Integer age;
          }
          @Embeddable
          class UserId implements Serializable {//此處Serializable是必須的
            String firstName;
            String lastName;
          }

          4.@Access(AcessType.PROPERTY)
          必須定義getter/setter方法才能實現(xiàn)持久化
          還有另一種取值:AcessType.FILED,可以不定義getter/setter方法,也能實現(xiàn)持久化
          此annotation也可以定義字段。

          5.主鍵:
             A.單鍵
              @Id
              @GeneratedValue (generator = "identity")
              @GenericGenerator (name = "identity", strategy = "identity")
              或者
              @javax.persistence.SequenceGenerator(
              name="SEQ_STORE",
              sequenceName="my_sequence")
              @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
              其中:
               strategy取值為:
               AUTO - either identity column, sequence or table depending on the underlying DB
               TABLE - table holding the id
               IDENTITY - identity column
               SEQUENCE - sequence
             B.復(fù)合組鍵
              @Entity
           class Customer {
            @EmbeddedId CustomerId id;
            boolean preferredCustomer;
            @MapsId("userId")//user.id與customerId.userId 使用相同的值
            @JoinColumns({
              @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
              @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
            })
            @OneToOne User user;
          }


          @Embeddable
          class CustomerId implements Serializable {
            UserId userId;
            String customerNumber;
          }


          @Entity
          class User {
            @EmbeddedId UserId id;
            Integer age;
          }

          @Embeddable
          class UserId implements Serializable {
            String firstName;
            String lastName;
          }
               

          6.字段設(shè)置:
          @Column(
              name="columnName";
              boolean un(2)ique() default false;
              boolean nu(3)llable() default true;
              boolean in(4)sertable() default true;
              boolean up(5)datable() default true;
              String col(6)umnDefinition() default "";
              String tab(7)le() default "";
              int length(8)() default 255;
              int precis(9)ion() default 0; // decimal precision
              int scale((10)) default 0; // decimal scale
          @Transient 非持久化字段
          @Basic 持久化字段
          @Basic(fetch = FetchType.LAZY) basic 用于定義property的fetch屬性
          @Enumerated(EnumType.STRING) 標(biāo)識enum persisted as String in database
          @Lob  blob clob字段
          @Formula("obj_length * obj_height * obj_width")//自定義輸出
          public long getObjectVolume()

          7.Mapping關(guān)系
          A.一對多或者一對一:
           @OneToOne(cascade = CascadeType.ALL) 一對一關(guān)系,級聯(lián)關(guān)系為all
           @PrimaryKeyJoinColumn或者
           指定關(guān)聯(lián)外鍵
           @JoinColumn(name="passport_fk")
           Passport passport,
           一對一的另一端只需@OneToOne(mappedBy = "passport"),passport為前一個實體聲明的名字


          @OneToMany(fetch = FetchType.LAZY , mappedBy = "adProduct")
          @Cascade(value = {CascadeType.ALL,CascadeType.DELETE_ORPHAN})

          @OrderBy(value = "id")  //排序
          B.多對一:
              @ManyToOne(fetch = FetchType.LAZY)
              @JoinColumn(name="adPosition_id",nullable=false)   
             

          8.Fetch and Lazy
          AnnotationsLazyFetch
          @[One|Many]ToOne](fetch=FetchType.LAZY) @LazyToOne(PROXY) @Fetch(SELECT)
          @[One|Many]ToOne](fetch=FetchType.EAGER) @LazyToOne(FALSE) @Fetch(JOIN)
          @ManyTo[One|Many](fetch=FetchType.LAZY) @LazyCollection(TRUE) @Fetch(SELECT)
          @ManyTo[One|Many](fetch=FetchType.EAGER) @LazyCollection(FALSE) @Fetch(JOIN)

          9.Cascade

          10.緩存

          緩存的注釋寫法如下,加在Entity的java類上:

          @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

          緩存的方式有四種,分別為:

          • CacheConcurrencyStrategy.NONE
          • CacheConcurrencyStrategy.READ_ONLY,只讀模式,在此模式下,如果對數(shù)據(jù)進(jìn)行更新操作,會有異常;
          • CacheConcurrencyStrategy.READ_WRITE,讀寫模式在更新緩存的時候會把緩存里面的數(shù)據(jù)換成一個鎖,其它事務(wù)如果去取相應(yīng)的緩存數(shù)據(jù),發(fā)現(xiàn)被鎖了直接就去數(shù)據(jù)庫查詢;
          • CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,不嚴(yán)格的讀寫模式則不會對緩存數(shù)據(jù)加鎖;
          • CacheConcurrencyStrategy.TRANSACTIONAL,事務(wù)模式指緩存支持事務(wù),當(dāng)事務(wù)回滾時,緩存也能回滾,只支持JTA環(huán)境。


          11.No-Annotation 字段:

                If the property is of a single type, it is mapped as @Basic

                Otherwise, if the type of the property is annotated as @Embeddable, it is mapped as @Embedded

                Otherwise, if the type of the property is Serializable, it is mapped as @Basic in a column holding the object in its serialized version

                Otherwise, if the type of the property is java.sql.Clob or java.sql.Blob, it is mapped as @Lob with the appropriate LobType
          posted on 2010-10-12 16:52 阮步兵 閱讀(3236) 評論(0)  編輯  收藏 所屬分類: OpenSource
          主站蜘蛛池模板: 西峡县| 拜泉县| 七台河市| 中阳县| 临沂市| 罗定市| 祥云县| 福贡县| 垫江县| 海安县| 博野县| 斗六市| 兴宁市| 临澧县| 肇州县| 内丘县| 余干县| 台中市| 南乐县| 新和县| 凤城市| 姚安县| 碌曲县| 辽宁省| 昭觉县| 容城县| 松江区| 东兰县| 桂东县| 扎囊县| 施甸县| 西安市| 合江县| 茂名市| 汤阴县| 清丰县| 江源县| 怀柔区| 仁怀市| 惠州市| 新民市|