<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <!-- 榪欎釜鏄嚜鍔ㄥ垱寤鴻〃鐨?nbsp;-->
<property name="openjpa.Log" value="DefaultLevel=WARN,SQL=TRACE" /> <!-- 榪欐槸鎵撳嵃SQL璇彞 -->
榪欎袱涓槸姣旇緝甯哥敤鐨勫睘鎬?br />
浠婂ぉ璇寸殑鏄竴瀵瑰鐨勫叧鑱旓紝榪欓噷鏈変笁涓被錛岄潪鍒槸User錛孭ost錛孋omment浠栦滑涔嬮棿鐨勫叧緋繪槸錛孶ser涓湁澶氫釜Post鍜?
Comment錛孭ost鏈夊涓狢omment錛岃孋omment鏈夊彧鏈変竴涓猆ser鍜孭ost錛岃繖閲屾垜灝變笉鍒楀嚭鏉ヨ〃緇撴瀯浜嗭紝鍥犱負鍙互鐢↗PA鐨勮嚜鍔ㄧ敓鎴?
琛?br />
涓嬮潰鏄疷ser鐨勫叧閿唬鐮?
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 }
娌℃湁浠涔堝彲璇寸殑錛屽彧璇翠袱鐐?br />
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榪樻湁鍙﹀涓涓睘鎬enerator榪欎釜鎴戝彧鍦ˋUTO涓嬪疄楠屼簡錛屼粬鏈変笅闈㈠嚑縐嶏細
-
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),綰ц仈錛屾垜榪欓噷鏄墍鏈夛紝綰ц仈涔熸槸鏈?涓紙榪樻槸鑰冪殑瀹樻柟鏂囨。錛夛細
-
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.
鍏蜂綋鐨勮繕鏄鐪嬪畼鏂規枃妗o紝璇寸殑寰堣緇?br />
榪欓噷涔熷彲浠ヤ嬌鐢ㄥ叾涓殑鍑犵錛?縐嶆垨鐫澶氶噸錛屼緥濡傦細@ManyToOne(cascade=
{CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.MERGE})
涓嬫潵鏄疨ost鍜孋omment鐨勫叧閿唬鐮侊細
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 }
涓嬫潵鐨勫簲璇ユ病鏈変粈涔堝彲璇寸殑錛屼笅闈㈡槸鎴戠殑嫻嬭瘯浠g爜錛?br />
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("鐢?/span>");
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錛?鐨勶級:JTATest.7z

]]>