• JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)


    一、结构

    二、代码

    1.

     1 package org.jpwh.model.collections.setofstrings;
     2 
     3 import org.jpwh.model.Constants;
     4 
     5 import javax.persistence.CollectionTable;
     6 import javax.persistence.Column;
     7 import javax.persistence.ElementCollection;
     8 import javax.persistence.Entity;
     9 import javax.persistence.GeneratedValue;
    10 import javax.persistence.Id;
    11 import javax.persistence.JoinColumn;
    12 import java.util.HashSet;
    13 import java.util.Set;
    14 
    15 @Entity
    16 public class Item {
    17 
    18     @Id
    19     @GeneratedValue(generator = Constants.ID_GENERATOR)
    20     protected Long id;
    21 
    22     @ElementCollection
    23     @CollectionTable(
    24             name = "IMAGE", // Defaults to ITEM_IMAGES
    25             joinColumns = @JoinColumn(name = "ITEM_ID")) // Default, actually
    26     @Column(name = "FILENAME") // Defaults to IMAGES
    27     protected Set<String> images = new HashSet<String>(); // Initialize field here
    28 
    29     public Long getId() {
    30         return id;
    31     }
    32 
    33     public Set<String> getImages() {
    34         return images;
    35     }
    36 
    37     public void setImages(Set<String> images) {
    38         this.images = images;
    39     }
    40 
    41     // ...
    42 }

    2.

     1 package org.jpwh.test.collections;
     2 
     3 import org.jpwh.env.JPATest;
     4 import org.jpwh.model.collections.setofstrings.Item;
     5 import org.testng.annotations.BeforeClass;
     6 import org.testng.annotations.Test;
     7 
     8 import javax.persistence.EntityManager;
     9 import javax.transaction.UserTransaction;
    10 
    11 import static org.testng.Assert.assertEquals;
    12 
    13 public class SetOfStrings extends JPATest {
    14 
    15     @Override
    16     public void configurePersistenceUnit() throws Exception {
    17         configurePersistenceUnit("SetOfStringsPU");
    18     }
    19 
    20     @Test
    21     public void storeLoadCollection() throws Exception {
    22         UserTransaction tx = TM.getUserTransaction();
    23         try {
    24             tx.begin();
    25             EntityManager em = JPA.createEntityManager();
    26             Item someItem = new Item();
    27 
    28             someItem.getImages().add("foo.jpg");
    29             someItem.getImages().add("bar.jpg");
    30             someItem.getImages().add("baz.jpg");
    31             someItem.getImages().add("baz.jpg"); // Duplicate, filtered at Java level by HashSet!
    32 
    33             em.persist(someItem);
    34             tx.commit();
    35             em.close();
    36             Long ITEM_ID = someItem.getId();
    37 
    38             tx.begin();
    39             em = JPA.createEntityManager();
    40             Item item = em.find(Item.class, ITEM_ID);
    41             assertEquals(item.getImages().size(), 3);
    42             tx.commit();
    43             em.close();
    44         } finally {
    45             TM.rollback();
    46         }
    47     }
    48 
    49 }

    It doesn’t seem likely that you’d allow the user to attach the same image more than once to the same item,

  • 相关阅读:
    程序命名规则
    CSS样式常用命名参考
    转:数据挖掘资料收集
    javascript占位符
    网站目录,文件夹命名规范
    IIS HTTP 500 内部服务器错误完美解决 IIS 服务器无法加载应用程序 '/LM/W3SVC/1/ROOT'。错误是 '没有注册类别
    人事工资合同管理系统菜单截图
    Vs 正则表达式 查找替换 微软权威参考
    什么是DNS,A记录,子域名,CNAME别名,MX记录,TXT记录,SRV 记录,TTL值
    MT主机控制面板Plesk 使用指南
  • 原文地址:https://www.cnblogs.com/shamgod/p/5369082.html
Copyright © 2020-2023  润新知