<property name="openjpa.Log" value="DefaultLevel=WARN,SQL=TRACE" /> <!-- 這是打印SQL語句 -->
今天說的是一對多的關聯,這里有三個類,非別是User,Post,Comment他們之間的關系是,User中有多個Post和 Comment,Post有多個Comment,而Comment有只有一個User和Post,這里我就不列出來表結構了,因為可以用JPA的自動生成 表
下面是User的關鍵代碼:
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
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 asuuid-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 asuuid-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 theEntityManager
finds a field that references a new entity during flush, and the field does not useCascadeType.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的關鍵代碼:
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 }
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();