或通过annotation声明ORM

Guzz JPA Annotation

Guzz支持JPA定义的注解,并参照hibernate进行了注解扩展,以满足guzz特性功能的声明需要。

对于JPA自身的注解,使用方式与JPA规范规定的相同;对于扩展的注解,命名和使用方式与hibernate相同(除了包名不一样,其他几乎无差别)。

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,用于编程时按名称引用。

一个典型域对象ORM定义如下:

 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完全参考手册

配置annotated的领域对象到系统中

对于hbm.xml的配置文件,通过guzz.xml中配置:

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" />

来声明。

同样的道理,对于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。如果您有其他示例补充,请通过本页评论告诉我们。

posted @ 2015-11-13 18:49  coolbing  阅读(309)  评论(0编辑  收藏  举报