JPA各種型別對映處理

2022-07-04 23:54:10 字數 1674 閱讀 2879

1.日期格式型別欄位的對映,利用@temporal(temporaltype.date)進行註解;例如:

private date birthday;

@temporal(temporaltype.date)

public date getbirthday()

public void setbirthday(date birthday)

2.列舉型別的對映,利用@enumerated,其引數enumtype表示指定存放在資料表中的形式,整型還是string;

首先建立乙個列舉:

public enum gender

在實體類中呼叫:

private gender gender = gender.male;  // 為列舉設定預設值

@enumerated(enumtype.string)

public gender getgender()

public void setgender(gender gender)

3.大文字資料型別的對映,例如:網路日誌,字串型別的長度顯然不夠,利用@lob註解,該註解用在字串型別之上在資料庫生成longtext型別的資料;例如:

private string diary;

@lob

public string getdiary()

public void setdiary(string diary)

4.@lob註解用在byte陣列型別,例如:儲存乙個檔案可以用此型別,用在這個上面在資料庫中可以生成longbolb資料型別;例如:

private byte file;

@lob

public byte getfile()

public void setfile(byte file)

5.如果在實體類中不需要該字段與資料庫中的表進行對映,但是預設的情況下是將實體類的全部字段對映成資料表的列,那該怎樣做呢?利用@transient註解,例如:

private string other;

@transient

public string getother()

public void setother(string other)

6.如果乙個實體類中包含乙個大資料型別的字段,如byte型別,當我們查詢該實體類時不得不查詢出該資料型別的字段,如果我們在查詢時只用到乙個其它欄位的資料,但是預設情況下是查詢全部的,那該怎樣避免查詢該大資料型別的資料呢?利用@basic註解進行標註,將fetch屬性值設定為lazy即可,這樣只有在我們呼叫該屬性的get方法時才會進行載入;

private byte file;

@lob @basic(fetch=fetchtype.lazy)

public byte getfile()

public void setfile(byte file)

7.@column    該註解表示資料表的對映列,放在屬性的getter方法上:

· length  該屬性表示該對映列的長度;

· nullable  該屬性表示該列是否可為空,true表示可為空,false表示不可為空;

· name   該屬性表示為該列起別名,不讓實體類的屬性名與資料庫的列名相同;

8.@table  該註解表示對映的表;放在該實體類之上:

· name  該屬性表示為表起別名,讓實體類與資料表名不相同;

JPA各種型別對映處理

1.日期格式型別欄位的對映,利用 temporal temporaltype.date 進行註解 例如 private date birthday temporal temporaltype.date public date getbirthday public void setbirthday d...

各種型別的轉換

int i 100 long l 2001 float f 300.2 double d 12345.119 char username csdn char temp 200 char buf cstring str variant t v1 bstr t v2 一 其它資料型別轉換為字串 二 字串...

python各種型別轉換

int x base 將x轉換為乙個整數 long x base 將x轉換為乙個長整數 float x 將x轉換到乙個浮點數 complex real imag 建立乙個複數 str x 將物件 x 轉換為字串 repr x 將物件 x 轉換為表示式字串 eval str 用來計算在字串中的有效py...