天天看點

JPA基本屬性注解

JPA(Java Persistence API)是Sun官方提出的Java持久化規範。它為Java開發人員提供了一種對象/關系映射工具來管理Java應用中的關系資料

JPA規範要求在類路徑的META-INF目錄下放置persistence.xml

JPA 中将一個類注解成實體類(entity class)有兩種不同的注解方式:基于屬性(property-based)和基于字段(field-based)的注解

基于字段的注解, 就是直接将注解放置在實體類的字段的前面

基于屬性的注解, 就是直接将注解放置在實體類相應的getter方法前面(這一點和Spring正好相反),但是同一個實體類中必須并且隻能使用其中一種注解方式

Entity、Table、Id、GeneratedValue、Basic、Column、Temporal、Transient、Lob、Transient、SecondaryTable、Embeddable、Embedded

JPA注解

(1)Entity

@javax.persistence.Entity(name="xxx") 

name指定實體Bean的名稱,預設值為 bean class 的非限定類名,select o from xxx o where o.id=?1

(2)Table

@javax.persistence.Table(catalog="xx",name="xx",schema="xx",uniqueConstraints={ @UniqueConstraint(columnNames={"xx","xx"})})

name:指定表的名稱

catalog:指定資料庫名稱

schema:指定資料庫的使用者名

uniqueConstraints:指定唯一性字段限制,如為personid 和name 字段指定唯一性限制

uniqueConstraints={ @UniqueConstraint(columnNames={"personid", "name"})}

(3)Id

@javax.persistence.Id()

映射到資料庫表的主鍵的屬性,一個實體隻能有一個屬性被映射為主鍵.

(4)GeneratedValue

@javax.persistence.GeneratedValue(generator="xxx",strategy=GenerationType.AUTO)

strategy:表示主鍵生成政策,有AUTO,INDENTITY,SEQUENCE 和 TABLE 4種

分别表示讓ORM架構自動選擇,根據資料庫的Identity字段生成,根據資料庫表的Sequence字段生成,以有根據一個額外的表生成主鍵,預設為AUTO 

generator:表示主鍵生成器的名稱,這個屬性通常和ORM架構相關,例如,Hibernate可以指定uuid等主鍵生成方式. 

Hibernate UUID

@Id @GeneratedValue(generator="system-uuid")

@GenericGenerator(name="system-uuid",strategy = "uuid")

(5)Basic

@javax.persistence.Basic(fetch=FetchType.LAZY,optional=true)

fetch:抓取政策,延時加載與立即加載

optional:指定在生成資料庫結構時字段是否允許為 null

(6)Column

@javax.persistence.Column(length=15,nullable=false,columnDefinition="",insertable=true,scale=10,table="",updatable=true)

@Column注解指定字段的詳細定義

name:字段的名稱,預設與屬性名稱一緻 

nullable:是否允許為null,預設為true

unique:是否唯一,預設為false 

length:字段的長度,僅對String類型的字段有效 

columnDefinition:表示該字段在資料庫中的實際類型

通常ORM架構可以根據屬性類型自動判斷資料庫中字段的類型,

但是對于Date類型仍無法确定資料庫中字段類型究竟是DATE,TIME還是TIMESTAMP,

此外,String的預設映射類型為VARCHAR,如果要将String類型映射到特定資料庫的BLOB或TEXT字段類型,該屬性非常有用

如: @Column(name="BIRTH",nullable="false",columnDefinition="DATE") 

insertable:預設情況下,JPA持續性提供程式假設所有列始終包含在 SQL INSERT 語句中。

如果該列不應包含在這些語句中,請将 insertable 設定為 false 

updatable:列始終包含在 SQL UPDATE 語句中。如果該列不應包含在這些語句中,請将 updatable 設定為 false 

table:實體的所有持久字段都存儲到一個其名稱為實體名稱的資料庫表中,如果該列與 @SecondaryTable表關聯

需将 name 設定為相應輔助表名稱的String名稱

(7)Temporal

@javax.persistence.Temporal(TemporalType.DATE)

value:TemporalType.DATE,TemporalType.TIME,TemporalType.TIMESTAMP時間類型格式

(8)Enumerated

@javax.persistence.Enumerated(EnumType.STRING)

value:EnumType.STRING,EnumType.ORDINAL

枚舉類型成員屬性映射,EnumType.STRING指定屬性映射為字元串,EnumType.ORDINAL指定屬性映射為資料序

(9)Lob

@javax.persistence.Lob

用于标注字段類型為Clob和Blob類型

Clob(Character Large Ojects)類型是長字元串類型,實體的類型可為char[]、Character[]、或者String類型

Blob(Binary Large Objects)類型是位元組類型,實體的類型可為byte[]、Byte[]、或者實作了Serializable接口的類。

通常使用惰性加載的方式, @Basic(fetch=FetchType.LAZY)

(10)Transient

@javax.persistence.Transient

@Transient表示該屬性并非一個到資料庫表的字段的映射,ORM架構将忽略該屬性

(11)SecondaryTable 

@javax.persistence.SecondaryTable

将一個實體映射到多個資料庫表中

如:

@Entity

@SecondaryTables({ 

@SecondaryTable(name = "Address"), 

    @SecondaryTable(name = "Comments") 

})

public class Forum implements Serializable {

@Column(table = "Address", length = 100) 

private String street; 

@Column(table = "Address", nullable = false) 

private String city; 

@Column(table = "Address") 

private String conutry; 

@Column(table = "Comments") 

private String title; 

@Column(table = "Comments") 

private String Comments; 

@Column(table = "Comments") 

}

table屬性的值指定字段存儲的表名稱

沒有用 @Column 注解改變屬性預設的字段将會存在于 Forum 表

(12)@Embeddable

@javax.persistence.Embeddable

嵌套映射,在被嵌套的類中使用Embeddable注解,說明這個就是一個可被嵌套的類,使用 @Embedded

當同一個類被不同的注解方式的類嵌套時,可能會出現一些錯誤,使用 @Access(AccessType. FIELD)設定被嵌套類的注解方式 

================================================================================================

(1)

@Entity注解定義

@Target(TYPE) @Retention(RUNTIME)

public @interface Entity{

    String name() default ""; //實體bean的名稱

}

(2)

@Table注解定義

@Target(value = {ElementType.TYPE}) 

@Retention(value = RetentionPolicy.RUNTIME) 

public @interface Table { 

public String name() default ""; //表的名稱

public String catalog() default ""; //資料庫名稱

public String schema() default ""; //資料庫使用者名

public UniqueConstraint[] uniqueConstraints() default {}; //指定多個字段唯一性限制 

}

(3)

@UniqueConstraint注解定義

public @interface UniqueConstraint{

    String[] columnNames( ); //唯一字段屬性名稱

}

(4)

@Id注解定義

@Target({METHOD, FIELD}) @Retention(RUNTIME) 

public @interface Id{ }

(5)

@注解GeneratedValue定義

@Target({METHOD, FIELD}) @Retention(RUNTIME)

public @interface GeneratedValue{

    GenerationType strategy() default AUTO; //主鍵生成政策

    String generator() default "";

}

(6)

@Column注解定義

@Target(value = {ElementType.METHOD, ElementType.FIELD}) 

@Retention(value = RetentionPolicy.RUNTIME) 

public @interface Column { 

public String name() default ""; //資料庫中的列名

public boolean unique() default false; //該列是否唯一

public boolean nullable() default true; //是否可以為空

public boolean insertable() default true; 

public boolean updatable() default true; 

public String columnDefinition() default ""; 

public String table() default ""; 

public int length() default 255; //該列的最大長度

public int precision() default 0; 

public int scale() default 0; 

}

(7)

@Temporal注解定義

public enum TemporalType{

    DATE, //代表 date類型 java.sql.Date 2008-08-08 

    TIME, //代表時間類型  java.sql.Time 20:00:00

    TIMESTAMP //代表時間 java.sql.Timestamp 2008-08-08 20:00:00.000000001

}

public enum TemporalType{

    DATE, //代表 date類型 //java.sql.Date 2008-08-08 

    TIME, //代表時間類型   //java.sql.Time 20:00:00

    TIMESTAMP //代表時間 //java.sql.Timestamp 2008-08-08 20:00:00.000000001

}

轉載自  http://blog.csdn.net/ljhabc1982/article/details/6556349

中場休息 ===========================================================

Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解釋

一:配置web.xml

      1)問題:spring項目中有多個配置檔案mvc.xml   dao.xml

      2)解決:在web.xml中

       <init-param>

             <param-name>contextConfigLocation</param-name>

             <param-value>/WEB-INF/xxx  

  •     public static final String ENTITY_NAME = "entity_name";  
  •   public Serializable generate(SessionImplementor session, Object object)   
  •     throws HibernateException;  
  • }