或經過annotation聲明ORM

Guzz JPA Annotation

Guzz支持JPA定義的註解,並參照hibernate進行了註解擴展,以知足guzz特性功能的聲明須要。java

對於JPA自身的註解,使用方式與JPA規範規定的相同;對於擴展的註解,命名和使用方式與hibernate相同(除了包名不同,其餘幾乎無差異)。sql

Guzz僅支持單對象ORM映射,不支持對象之間的關聯映射(one-one, one-many, many-to, many-many),也不支持JPA的持久化方法。guzz annotation僅用來替代hbm.xml配置文件。數據庫

聲明annotation

在guzz中通常須要在POJO中聲明最少2個annotation:@javax.persistence.Entity和@org.guzz.annotations.Entity。編程

前者爲JPA規範要求,後者用於聲明對象的businessName,用於編程時按名稱引用。oracle

一個典型域對象ORM定義以下:dom

 1 package org.guzz.test;
 2 
 3 import java.util.Date;
 4 
 5 import javax.persistence.Column;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.GenerationType;
 8 import javax.persistence.TableGenerator;
 9 
10 import org.guzz.annotations.Table;
11 
12 
13 @javax.persistence.Entity
14 @org.guzz.annotations.Entity(businessName = "comment")
15 @Table(name="TB_COMMENT", shadow = CommentShadowView.class)
16 @TableGenerator(
17                 name = "commentGen",
18                 table="tb_id",
19                 catalog="somelog",
20                 schema="some_schema",
21                 pkColumnName="pk",
22                 pkColumnValue="2",
23                 valueColumnName="id_count",
24                 initialValue=100,
25                 allocationSize=20
26                 /*
27                  * create table tb_id(pk int(11) primary key, id_count int(11) default 0)
28                  * insert into tb_id(pk, id_count) values(2, 100)
29                  */
30 )
31 public class Comment {
32 
33         @javax.persistence.Id
34         @GeneratedValue(generator="commentGen", strategy=GenerationType.TABLE)
35         private int id ;
36         
37         private int userId ;
38         
39         private String userName ;
40         
41         //@javax.persistence.Basic(fetch=FetchType.LAZY)
42         @Column(name="DESCRIPTION")
43         private String content ;
44         
45         private Date createdTime ;
46 
47         public int getId() {
48                 return id;
49         }
50 
51         public void setId(int id) {
52                 this.id = id;
53         }
54         
55         //other gets/sets...
56 }

注意: 每一個域對象都必須包含一個主鍵,經過@javax.persistence.Id聲明。同時guzz將按照主鍵聲明在變量上仍是getXxx方法上決定按照field仍是property構造映射屬性。測試

Guzz支持的JPA Annotation,以及Guzz自身擴展的Annotation的詳細使用說明,請參看Guzz JPA Annotation徹底參考手冊fetch

配置annotated的領域對象到系統中

對於hbm.xml的配置文件,經過guzz.xml中配置:ui

1 <business name="user" dbgroup="default" class="org.guzz.test.User" interpret="" file="classpath:org/guzz/test/User.hbm.xml" />
2 <business name="book" class="org.guzz.test.Book" file="classpath:org/guzz/test/Book.hbm.xml" />

來聲明。this

一樣的道理,對於annotated的領域對象,也須要在guzz.xml主配置文件中進行聲明。聲明的標籤爲:a-business。

每一個a-business標籤對應1個領域對象。a-business擁有3個屬性:

 

屬性名 Required 做用
class Required annotated領域對象的類全名稱(fully qualified class name)
name Optional business name。若是填寫,則覆蓋註解中的businessName
dbgroup Optional 對象存儲到的數據庫組。若是填寫,則覆蓋註解中的dbGroup

 

一個典型的配置以下:

1  <a-business name="user" dbgroup="default" class="org.guzz.test.User"/>
2         <a-business name="book" class="org.guzz.test.Book" />
3         <a-business name="userInfo" dbgroup="oracle" class="org.guzz.test.UserInfo" />
4         <a-business name="userInfo2" dbgroup="default" class="org.guzz.test.UserInfoH2" />
5         <a-business name="comment" dbgroup="default" class="org.guzz.test.Comment"/>
6         <a-business name="cargo" class="org.guzz.test.shop.Cargo" />
7         <a-business name="sp" class="org.guzz.test.shop.SpecialProperty" />

其中,每一個領域對象的定義能夠在guzz測試源代碼中找到。

 

主鍵使用示例

native(Mysql和H2中爲自增):

 1 @javax.persistence.Entity
 2 @org.guzz.annotations.Entity(businessName="user")
 3 @Table(name="tb_user")
 4 public class User implements Serializable {
 5         
 6         private int id ;
 7         ...
 8         
 9         @javax.persistence.Id
10         @GeneratedValue(strategy=GenerationType.AUTO)
11         public int getId() {
12                 return id;
13         }
14         ...

assigned:

 1 @javax.persistence.Entity
 2 @Table(name="tb_user")
 3 public class User implements Serializable {
 4         
 5         private int id ;
 6         ...
 7         
 8         @javax.persistence.Id
 9         @GenericGenerator(name = "assignedGen", strategy = "assigned")
10         @GeneratedValue(generator = "assignedGen")
11         public int getId() {
12                 return id;
13         }
14         ...

sequence:

 1 @javax.persistence.Entity
 2 @Table(name="tb_user")
 3 public class User implements Serializable {
 4         
 5         private int id ;
 6         ...
 7         
 8         @javax.persistence.Id
 9         @Column(name="pk")
10         @GeneratedValue(generator="userIdGen")
11         @GenericGenerator(name="userIdGen", strategy="sequence", parameters={@Parameter(name="sequence", value="seq_user_id")}
12         )
13         public id getId() {
14                 return id;
15         }
16         ...

random:

 1 @javax.persistence.Entity
 2 @Table(name="tb_invitation_key")
 3 public class InvitationKey implements Serializable {
 4         
 5         private String id ;
 6         ...
 7         
 8         @javax.persistence.Id
 9         @GeneratedValue(generator="randomGen")
10         @GenericGenerator(name = "randomGen", strategy = "random", parameters={@Parameter(name = "length", value = "32")})
11         public String getId() {
12                 return id;
13         }
14         ...

uuid:

 1 @javax.persistence.Entity
 2 @Table(name="tb_invitation_key")
 3 public class InvitationKey implements Serializable {
 4         
 5         private String id ;
 6         ...
 7         
 8         @javax.persistence.Id
 9         @GeneratedValue(generator="uuidGen")
10         @GenericGenerator(name = "uuidGen", strategy = "uuid")
11         public String getId() {
12                 return id;
13         }
14         ...

其餘主鍵生成策略,以及策略詳細參數,請閱讀 TutorialHbmXml。若是您有其餘示例補充,請經過本頁評論告訴咱們。

相關文章
相關標籤/搜索