JPA'Many2Many

Many2Many关系:
 Document(主动)<---->(被动)KMFile

 

class Document
主动关联设置:指定中间表,及字段。
 @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST}, targetEntity=KMFile.class)
 @JoinTable(inverseJoinColumns={@JoinColumn(name="file_id")}, name="LINK_KMFile", joinColumns={@JoinColumn(name="linker_id")})
 public List<KMFile> getFiles() {
  return fileList;
 }

 

class KMFile
被动关联设置:只需设置mappedBy,即本体在主体中的属性(Document#getFiles)。
 @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST}, mappedBy="files", targetEntity=Document.class)
 public List<Document> getDocuments() {
  return docList;
 }

  

使用示例:

public class Test{ static EntityManagerFactory emf = Persistence .createEntityManagerFactory(GlobConfig.PERSISTENCE_UNIT_NAME); static EntityManager em = emf.createEntityManager(); public static void main(String[] a) throws Exception { em.getTransaction().begin(); // 持久化对象 long docId = UUIDUtil.uuid(); Document doc = new Document(); doc.setId(docId); doc.setTitle("AAA"); long fileId = UUIDUtil.uuid(); KMFile file = new KMFile(); file.setId(fileId); file.setTitle("BBB"); //这里也可以:file.getDocuments().add(doc);em.persist(file);即持久化无主动关联、被动关联之区分。 doc.getFiles().add(file); em.persist(doc); em.flush(); // 持久化关系(必要,否则关联表无记录):注意需使用查询返回对象(带jpa session信息) file = em.find(KMFile.class, fileId); doc = em.find(Document.class, docId); file.getDocuments().add(doc); //下面这条不能执行,因为之前已持久化doc.getFiles().add(file); //如果持久化之前doc没有添加getFiles().add,即各自独立保存,那么维持关系要相互add。 // doc.getFiles().add(file); //查询验证 Query query = em.createQuery("SELECT d FROM Document d where d.id=:docId"); query.setParameter("docId", docId); Document d=(Document)query.getSingleResult(); System.out.println("文档:"+d.getTitle()); d.getFiles().size(); System.out.println("文档-附件:"+d.getFiles().get(0).getTitle()); query = em.createQuery("SELECT f FROM KMFile f where f.id=:fileId"); query.setParameter("fileId", fileId); KMFile f=(KMFile)query.getSingleResult(); System.out.println("附件:"+f.getTitle()); f.getDocuments().size(); System.out.println("附件-文档:"+f.getDocuments().get(0).getTitle()); em.getTransaction().commit(); em.close(); emf.close(); } }

Logger调为Fine,以输出SQL(本例使用derby),执行结果输出:

--INSERT INTO Document (ID, CREATETIME, LASTMODIFIED, TITLE, DESCRIPTION, OWNER_ID) VALUES (?, ?, ?, ?, ?, ?)
 bind => [4508551237913941891, null, null, AAA, null, null]
--INSERT INTO KMFile (ID, CREATETIME, NAME, TYPE, SIZE, LASTMODIFIED, TITLE) VALUES (?, ?, ?, ?, ?, ?, ?)
 bind => [4508551237913941892, null, null, 0, 0, null, BBB]
--INSERT INTO LINK_KMFile (file_id, linker_id) VALUES (?, ?)
 bind => [4508551237913941892, 4508551237913941891]


--SELECT ID, CREATETIME, LASTMODIFIED, TITLE, DESCRIPTION, OWNER_ID FROM Document WHERE (ID = CAST (? AS BIGINT ))
 bind => [4508551237913941891]
文档:AAA
文档-附件:BBB
--SELECT ID, CREATETIME, NAME, TYPE, SIZE, LASTMODIFIED, TITLE FROM KMFile WHERE (ID = CAST (? AS BIGINT ))
 bind => [4508551237913941892]
附件:BBB
附件-文档:AAA

 

Relationships: http://www.devx.com/Java/Article/33906/0/page/2

你可能感兴趣的:(jpa,File,null,query,文档,linker)