JPA註解查詢

2021-06-29 03:15:06 字數 4859 閱讀 9166

1、@entity(name="entityname")

必須,name為可選,對應資料庫中一的個表

@entity //標識這個pojo是乙個jpa實體    

public class

users

implements

serializable

2、@table(name="",catalog="",schema="")

可選,通常和@entity配合使用,只能標註在實體的class定義處,表示實體對應的資料庫表的資訊

name:可選,表示表的名稱.預設地,表名和實體名稱一致,只有在不一致的情況下才需要指定表名

catalog:可選,表示catalog名稱,預設為catalog("").

schema:可選,表示schema名稱,預設為schema("").

@entity

@table(name =

"users"

) //指定表名為users    

public class

users

implements

serializable

3、@id

必須,@id定義了對映到資料庫表的主鍵的屬性,乙個實體只能有乙個屬性被對映為主鍵.置於get***x()前.

public class

users

implements

serializable

6、@column

可選 @column描述了資料庫表中該字段的詳細定義,這對於根據jpa註解生成資料庫表結構的工具非常有作用.

name:表示資料庫表中該字段的名稱,預設情形屬性名稱一致

nullable:表示該欄位是否允許為null,預設為true

unique:表示該欄位是否是唯一標識,預設為false

length:表示該字段的大小,僅對string型別的字段有效

insertable:表示在orm框架執行插入操作時,該欄位是否應出現insetrt語句中,預設為true

updateable:表示在orm框架執行更新操作時,該欄位是否應該出現在update語句中,預設為true.對於一經建立就不可以更改的字段,該屬性非常有用,如對於birthday欄位.

columndefinition:表示該字段在資料庫中的實際型別.通常orm框架可以根據屬性型別自動判斷資料庫中字段的型別,但是對於date型別仍無法確定資料庫中欄位型別究竟是date,time還是timestamp.此外,string的預設對映型別為varchar,如果要將string型別對映到特定資料庫的blob或text欄位型別,該屬性非常有用.

@column(name = "user_code", nullable =false, length=32)//設定屬性usercode對應的字段為user_code,長度為32,非空    

private string

usercode;

@column(name = "user_wages", nullable =true, precision=12, scale=2

)//設定屬性wages對應的字段為user_wages,12位數字可保留兩位小數,可以為空    

private double

wages;

@temporal(temporaltype.

date

)//設定為時間型別    

private date

joindate;

7、@transient

可選 @transient表示該屬性並非乙個到資料庫表的字段的對映,orm框架將忽略該屬性.

如果乙個屬性並非資料庫表的字段對映,就務必將其標示為@transient,否則,orm框架預設其註解為@basic

@transient

private int tempvalue;    

public int gettempvalue()    

public void settempvalue(int value)   

8、@manytoone(fetch=fetchtype,cascade=cascadetype)

可選 @manytoone表示乙個多對一的對映,該註解標註的屬性通常是資料庫表的外來鍵

optional:是否允許該字段為null,該屬性應該根據資料庫表的外來鍵約束來確定,預設為true

fetch:表示抓取策略,預設為fetchtype.eager (急載入,表示載入屬性時馬上從資料庫中載入,與 懶載入fetchtype.lazy對應)

cascade:表示預設的級聯操作策略,可以指定為all,persist,merge,refresh和remove中的若干組合,預設為無級聯操作

targetentity:表示該屬性關聯的實體型別.該屬性通常不必指定,orm框架根據屬性型別自動判斷targetentity.

9、@onetomany(fetch=fetchtype,cascade=cascadetype)

可選 @onetomany描述乙個一對多的關聯,該屬性應該為集體型別,在資料庫中並沒有實際欄位.

fetch:表示抓取策略,預設為fetchtype.lazy,因為關聯的多個物件通常不必從資料庫預先讀取到記憶體

cascade:表示級聯操作策略,對於onetomany型別的關聯非常重要,通常該實體更新或刪除時,其關聯的實體也應當被更新或刪除

例如:實體user和order是onetomany的關係,則實體user被刪除時,其關聯的實體order也應該被全部刪除

有t_one和t_many兩個表,他們是一對多的關係,註解範例如下

主pojo

@entity    

@table(name = "t_one")    

public class one implements serializable , inversejoincolumns = )    

private collectionmanybidcollection;    

(舉例解釋:

@entity

public class customer implements serializable

customer 通過 customerpassports 關聯表和 passport 關聯。該關聯表通過 passport_fk 外來鍵指向 passport 表,該信心定義為 inversejoincolumns 的屬性值。 通過 customer_fk 外來鍵指向 customer 表,該資訊定義為 joincolumns 屬性值。

)第二個pojo

@entity    

@table(name = "t_manyb")    

public class manyb implements serializable  

@entity

public class engineer

extends employee

@entity

public class manager

extends employee

14、@embedded

可選 @embedded將幾個字段組合成乙個類,並作為整個entity的乙個屬性.

例如user包括id,name,city,street,zip屬性.

我們希望city,street,zip屬性對映為address物件.這樣,user物件將具有id,name和address這三個屬性.

address物件必須定義為@embededable

@embeddable

public class address

@entity

public class user

} 15、@orderby (公對list有效,如果set或map使用mapkey)

在載入資料的時候可以為被關聯實體中的乙個字段屬性指定順序,使用@orderby註解實現

@table(name = "users")

public class user

16、@lob

大字段

@lob

//對應blob欄位型別

@column(name = "photo")    

private serializable photo;    

@lob //對應clob欄位型別

@column(name = "description")    

private string description;

hibernate驗證註解

註解 適用型別

說明 示例

@pattern

string

通過正規表示式來驗證字串

@attern(regex=」[a-z]」)

@length

string

驗證字串的長度

@length(min=3,max=20)

@email

string

驗證乙個email位址是否有效

@email

@range

long

驗證乙個整型是否在有效的範圍內

@range(min=0,max=100)

@min

long

驗證乙個整型必須不小於指定值

@min(value=10)

@max

long

驗證乙個整型必須不大於指定值

@max(value=20)

@size

集合或陣列

集合或陣列的大小是否在指定範圍內

@size(min=1,max=255)

以上每個註解都可能性有乙個message屬性,用於在驗證失敗後向使用者返回的訊息,還可以三個屬性上使用多個註解 

JPA註解補充

fetchtype.lazy和 fetchtype.eager 什麼區別?1 fetchtype.lazy 懶載入,載入乙個實體時,定義懶載入的屬性不會馬上從資料庫中載入。2 fetchtype.eager 急載入,載入乙個實體時,定義急載入的屬性會立即從資料庫中載入。3 比方 user 類有兩個屬...

JPA實體註解

entity name entityname 必須,name為可選,對應資料庫中一的個表 table name catalog schema 可選,通常和 entity配合使用,只能標註在實體的class定義處,表示實體對應的資料庫表的資訊 name 可選,表示表的名稱。預設地,表名和實體名稱一致,...

JPA註解介紹

必須,name為可選,對應資料庫中一的個表 entity 標識這個pojo是乙個jpa實體 public class users implements serializable可選,通常和 entity配合使用,只能標註在實體的class定義處,表示實體對應的資料庫表的資訊 name 可選,表示表的...