这里接上次的一些没有说完的,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
天独 阅读(673)
评论(1) 编辑 收藏 所属分类:
Java