posts - 1,  comments - 1,  trackbacks - 0
          這里接上次的一些沒有說完的,openJPA的2兩個(gè)常用屬性
          <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <!-- 這個(gè)是自動(dòng)創(chuàng)建表的 -->
          <property name="openjpa.Log" value="DefaultLevel=WARN,SQL=TRACE" />  <!-- 這是打印SQL語句 -->
          這兩個(gè)是比較常用的屬性
          今天說的是一對多的關(guān)聯(lián),這里有三個(gè)類,非別是User,Post,Comment他們之間的關(guān)系是,User中有多個(gè)Post和 Comment,Post有多個(gè)Comment,而Comment有只有一個(gè)User和Post,這里我就不列出來表結(jié)構(gòu)了,因?yàn)榭梢杂肑PA的自動(dòng)生成 表
          下面是User的關(guān)鍵代碼:
           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 }
          沒有什么可說的,只說兩點(diǎn)
          1.@GeneratedValue(strategy = GenerationType.SEQUENCE)這是說為id自動(dòng)生成一個(gè)序列,這個(gè)選項(xiàng)湖自動(dòng)在數(shù)據(jù)庫中創(chuàng)建一個(gè)用來生成序列的表,一共有四種
          • 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官方文檔說的, 英文不好我就不翻譯了,應(yīng)該說的很明白,也很容易懂
          GenerationValue還有另外一個(gè)屬性generator這個(gè)我只在AUTO下實(shí)驗(yàn)了,他有下面幾種:
          • 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),級聯(lián),我這里是所有,級聯(lián)也是有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.

          具體的還是要看官方文檔,說的很詳細(xì)
          這里也可以使用其中的幾種,1種或著多重,例如:@ManyToOne(cascade= {CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.MERGE})
          下來是Post和Comment的關(guān)鍵代碼:
           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 }
          下來的應(yīng)該沒有什么可說的,下面是我的測試代碼:
           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 天獨(dú) 閱讀(693) 評論(1)  編輯  收藏 所屬分類: Java

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          相關(guān)文章:
           
          主站蜘蛛池模板: 文水县| 治多县| 金乡县| 宣恩县| 鸡东县| 苗栗县| 汶上县| 河北区| 弥渡县| 磐石市| 新丰县| 土默特左旗| 德惠市| 获嘉县| 永川市| 微博| 余江县| 西华县| 高尔夫| 报价| 香港| 盐城市| 茌平县| 鹰潭市| 博客| 安陆市| 连山| 中超| 宜黄县| 城市| 临清市| 牟定县| 绍兴市| 宁阳县| 洪江市| 吴忠市| 勃利县| 毕节市| 静海县| 西乌| 赤峰市|