hibernate 复合主键 多对多 级联保存

package com.patsnap.hibernate.beans;

import java.io.Serializable;
import java.util.List;

public class Patent implements Serializable{
 private String pn;
 
 private String apno;
 
 private List<LegalStatus> legalStatus;
 
 public List<LegalStatus> getLegalStatus() {
  return legalStatus;
 }
 public void setLegalStatus(List<LegalStatus> legalStatus) {
  this.legalStatus = legalStatus;
 }
 public String getPn() {
  return pn;
 }
 public void setPn(String pn) {
  this.pn = pn;
 }
 public String getApno() {
  return apno;
 }
 public void setApno(String apno) {
  this.apno = apno;
 }
}

 

package com.patsnap.hibernate.beans;

import java.util.List;

public class LegalStatus {
 private int id;
 
 private String apno;
 
 private List<Patent> patents;

 public List<Patent> getPatents() {
  return patents;
 }

 public void setPatents(List<Patent> patents) {
  this.patents = patents;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getApno() {
  return apno;
 }

 public void setApno(String apno) {
  this.apno = apno;
 }
}

 

<?xml version="1.0" encoding='UTF-8'?>    
<!DOCTYPE hibernate-mapping PUBLIC    
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.patsnap.hibernate.beans">
 <class name="Patent" table="Patent">
  <composite-id>
   <key-property name="pn" type="string" column="pn" />
   <key-property name="apno" type="string" column="apno" />
  </composite-id>

  <bag name="legalStatus" table="Patent_LegatStatus"
   inverse="false">
   <key>
    <column name="pn"></column>
    <column name="apno"></column>
   </key>
   <many-to-many class="LegalStatus" column="id" />
  </bag>
 </class>
</hibernate-mapping>  

 

<?xml version="1.0" encoding='UTF-8'?>    
<!DOCTYPE hibernate-mapping PUBLIC    
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.patsnap.hibernate.beans">
 <class name="LegalStatus" table="LegalStatus">
  <id name="id" column="id" type="int">
   <generator class="native" />
  </id>
  <property name="apno" column="apno" type="java.lang.String" />

  <bag name="patents" table="Patent_LegatStatus"
   inverse="false">
   <key>
    <column name="id"></column>
   </key>
   <many-to-many class="Patent">
    <column name="pn"></column>
    <column name="apno"></column>
   </many-to-many>
  </bag>
 </class>
</hibernate-mapping>  

 

 

注1:这里bag标签里没有设置cascade属性,这样在保存Patent或LegalStatus时仅级联保存中间表,不会删除中间表数据,但要求保存一方的时候,另一方的数据必须已经存在(因为仅级联保存中间表数据,不会保存另一方数据)

public static void insert() {
  Session session = HibernateSessionFactory.getSession();
  Transaction t = session.beginTransaction();

  LegalStatus legalStatus = new LegalStatus();
  legalStatus.setApno("12345");

  Patent patent = new Patent();
  patent.setPn("321141112");
  patent.setApno("3211112");//该patent在数据库必须已经存在,否则报错
  List<Patent> list = new ArrayList<Patent>();
  list.add(patent);
  legalStatus.setPatents(list);

  session.save(legalStatus);

  t.commit();

 }

注2:如果bag标签里cascade属性设置了相应值(如save-update等),则在保存一方数据的同时会保存同时保存中间表和另一方的数据(如果不存在的话),但是再保存两者关系的同时,会先把中间表数据删除一遍,再加上当前添加的关系,所以这时候要特别注意,要先把关系设置完整再做保存操作,否则会删除已有的关系

public static void insert(){
     Session session = HibernateSessionFactory.getSession();
     Transaction t = session.beginTransaction();
     
     
     LegalStatus legalStatus =new LegalStatus();
     legalStatus.setApno("12345");
     
     
     List<Patent> patentList = session.createQuery("from Patent where pn ='321141112' and apno='3211112'").list();
     Patent patent = new Patent();
     if (patentList.size()==1) {
      patent = patentList.get(0);
      patent.getLegalStatus().add(legalStatus);//保证原有的关系不变,不做这一步则丢失原有关系
  }else {
   patent.setPn("321141112");
   patent.setApno("3211112");
   List<Patent> list = new ArrayList<Patent>();
   list.add(patent);
   legalStatus.setPatents(list);
  }
         
     session.save(legalStatus);
     
     t.commit();
     
    }

注3:如果涉及到复合主键,该类需要implements Serializable

你可能感兴趣的:(Hibernate)