這里接上次的一些沒有說完的,openJPA的2兩個常用屬性
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <!-- 這個是自動創建表的 -->
<property name="openjpa.Log" value="DefaultLevel=WARN,SQL=TRACE" /> <!-- 這是打印SQL語句 -->
這兩個是比較常用的屬性
今天說的是一對多的關聯,這里有三個類,非別是User,Post,Comment他們之間的關系是,User中有多個Post和
Comment,Post有多個Comment,而Comment有只有一個User和Post,這里我就不列出來表結構了,因為可以用JPA的自動生成
表
下面是User的關鍵代碼:
1 @Entity
2 public class User {
3 @Id
4 @GeneratedValue(strategy = GenerationType.SEQUENCE)
5 private int id;
6 private String name;
7 private String password;
8 private String gender;
9 @OneToMany(cascade = CascadeType.ALL)
10 private List<Comment> comments;
11 @OneToMany(cascade = CascadeType.ALL)
12 private List<Post> posts;
13 // ………………
14 }
沒有什么可說的,只說兩點
1.@GeneratedValue(strategy =
GenerationType.SEQUENCE)這是說為id自動生成一個序列,這個選項湖自動在數據庫中創建一個用來生成序列的表,一共有四種
-
GeneratorType.AUTO
: The default.
Assign the field a generated value, leaving the details to the JPA
vendor.
-
GenerationType.IDENTITY
: The
database will assign an identity value on insert.
-
GenerationType.SEQUENCE
: Use a
datastore sequence to generate a field value.
-
GenerationType.TABLE
: Use a sequence
table to generate a field value
這事Apache官方文檔說的, 英文不好我就不翻譯了,應該說的很明白,也很容易懂
GenerationValue還有另外一個屬性generator這個我只在AUTO下實驗了,他有下面幾種:
-
uuid-string
: OpenJPA will generate a
128-bit type 1 UUID unique within the network, represented as a
16-character string. For more information on UUIDs, see the IETF UUID
draft specification at:
http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/
-
uuid-hex
: Same as uuid-string
, but represents the type 1 UUID as a
32-character hexadecimal string.
-
uuid-type4-string
: OpenJPA will
generate a 128-bit type 4 pseudo-random UUID, represented as a
16-character string. For more information on UUIDs, see the IETF UUID
draft specification at:
http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/
-
uuid-type4-hex
: Same as uuid-type4-string
, but represents the type 4
UUID as a 32-character hexadecimal string.
2.@OneToMany(cascade = CascadeType.ALL),級聯,我這里是所有,級聯也是有4中(還是考的官方文檔):
-
CascadeType.PERSIST
: When persisting
an entity, also persist the entities held in this field. We suggest
liberal application of this cascade rule, because if the EntityManager
finds a field that references a
new entity during flush, and the field does not use CascadeType.PERSIST
, it is an error.
-
CascadeType.REMOVE
: When deleting an
entity, also delete the entities held in this field.
-
CascadeType.REFRESH
: When refreshing
an entity, also refresh the entities held in this field.
-
CascadeType.MERGE
: When merging
entity state, also merge the entities held in this field.
具體的還是要看官方文檔,說的很詳細
這里也可以使用其中的幾種,1種或著多重,例如:@ManyToOne(cascade=
{CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.MERGE})
下來是Post和Comment的關鍵代碼:
1 @Entity
2 public class Post {
3 @Id
4 @GeneratedValue(strategy = GenerationType.SEQUENCE)
5 private int id;
6 private Date createDate;
7 private String title;
8 private String content;
9 @ManyToOne(cascade = CascadeType.ALL)
10 private User author;
11 @OneToMany(cascade = CascadeType.ALL)
12 private List<Comment> comments;
13 // ………………
14 }
15
16 @Entity
17 public class Comment {
18 @Id
19 @GeneratedValue(strategy = GenerationType.SEQUENCE)
20 private int id;
21 private Date createDate;
22 private String title;
23 private String content;
24 @ManyToOne(cascade = CascadeType.ALL)
25 private User author;
26 @ManyToOne(cascade = CascadeType.ALL)
27 private Post post;
28 // ………………
29 }
下來的應該沒有什么可說的,下面是我的測試代碼:
1 EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
2 EntityManager em = factory.createEntityManager();
3 em.getTransaction().begin();
4 User user = new User();
5 user.setName("Innate");
6 user.setPassword("Innate");
7 user.setGender("男");
8 List<Post> posts = new ArrayList<Post>();
9
10 for (int i = 0; i < 5; i++) {
11 Post post = new Post();
12 post.setAuthor(user);
13 post.setContent("test");
14 post.setCreateDate(new Date());
15 post.setTitle("Innate");
16 posts.add(post);
17 List<Comment> comments = new ArrayList<Comment>();
18 for (int j = 0; j < 5; j++) {
19 Comment comment = new Comment();
20 comment.setAuthor(user);
21 comment.setContent("testtest");
22 comment.setCreateDate(new Date());
23 comment.setPost(post);
24 comment.setTitle("InnateInnate");
25 comments.add(comment);
26 }
27 post.setComments(comments);
28 user.setComments(comments);
29 }
30 user.setPosts(posts);
31 em.persist(user);
32 em.getTransaction().commit();
源代碼(包含1,2的):
JTATest.7z
posted on 2010-04-06 22:24
天獨 閱讀(686)
評論(1) 編輯 收藏 所屬分類:
Java