<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 1,  trackbacks - 0
    這里接上次的一些沒有說完的,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

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


    網站導航:
     
    主站蜘蛛池模板: 内射无码专区久久亚洲| 美女黄频a美女大全免费皮| 91麻豆最新在线人成免费观看 | 亚洲国产精品综合久久久| 蜜桃成人无码区免费视频网站| 久久久久久久尹人综合网亚洲 | 日韩免费高清视频网站| 亚洲精品无码久久久久A片苍井空 亚洲精品无码久久久久YW | 亚洲人成无码网站| 两个人看的www高清免费观看| 亚洲综合AV在线在线播放| aa级毛片毛片免费观看久| 亚洲精品无码Av人在线观看国产| 91免费在线视频| 久久久久久a亚洲欧洲AV| 99re在线这里只有精品免费| 亚洲香蕉免费有线视频| 在线看片韩国免费人成视频| 亚洲国产综合精品中文第一| 成人免费一区二区三区在线观看| 亚洲欧美成人综合久久久| 在线观看91精品国产不卡免费| 美女18一级毛片免费看| 国产精品亚洲玖玖玖在线观看| 青柠影视在线观看免费| 中文字幕亚洲免费无线观看日本 | 国产亚洲午夜高清国产拍精品 | 黄网址在线永久免费观看| 老湿机一区午夜精品免费福利| 国产午夜亚洲不卡| 久久精品电影免费动漫| 亚洲福利视频网站| 成人男女网18免费视频| 国产精品久久亚洲一区二区| 久久久久无码专区亚洲av| 国产一精品一AV一免费| 亚洲校园春色小说| 无码国模国产在线观看免费| 日韩在线观看视频免费| 亚洲AV无码乱码国产麻豆 | a级毛片在线免费|