hibernate idbagマッピング


TeamとStudioは1対多の関係であると仮定し、studentにはteam_のみidとnameの2つの属性は、Studentエンティティクラスを確立せずにelement方式を採用することができ、bagは重複を許可することができるため、teamに基づいて次のstudentを削除するとき、hibernateはどのデータを削除するか分からないので、teamの下ですべてのstudentをすべて削除し、削除すべきでないデータを再挿入するだけです.これにより、効率に大きな影響を及ぼします.この場合、idbagラベルを使用して、studentにフィールドcidを追加して、各studentデータベース構造を一意に識別できます.
create table teamIDBag (id varchar(32),teamname varchar(32));
create table studentIDBag(team_id varchar(32),name varchar(32),cid varchar(32));
 
Pojo:
package Collection.IDBag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Team {
   private String id;
   private String teamname;
   private List students=new ArrayList();
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getTeamname(){
    return teamname;
}
public void setTeamname(String teamname) {
    this.teamname = teamname;
}
public List getStudents() {
    return students;
}
public void setStudents(List students){
    this.students = students;
}

}

  Team.hbm.xml
<?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">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="Collection.IDBag.Team" table="teamIDBag" >
    <id name="id" unsaved-value="null">
      <generator class="uuid.hex"></generator>
    </id>
    <property name="teamname" type="string" column="teamname"></property>
  
    <idbag name="students" table="studentIDBag" cascade="all,delete-orphan" >
      <collection-id type="string" column="cid">
        <generator class="uuid.hex"></generator>
      </collection-id>
      <key column="team_id"></key>
      <element type="string" column="name"></element>
    </idbag>
</class>

</hibernate-mapping>
 
Hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">
        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&amp;useUnicode=true
    </property>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">mysql</property>
    <property name="connection.password">1234</property>
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.show_sql">true</property>
    <property name="current_session_context_class">thread</property>
    <property name="jdbc.batch_size">15</property>
    <mapping resource="Collection/IDBag/Team.hbm.xml" />




</session-factory>

</hibernate-configuration>
 
テストコード:
package Collection.IDBag;


import java.io.File;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {


    public static void main(String[] args) {

        String filePath=System.getProperty("user.dir")+File.separator+"src/Collection/IDBag"+File.separator+"hibernate.cfg.xml";
        File file=new File(filePath);
        System.out.println(filePath);
        SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
        Session session=sessionFactory.openSession();
        Transaction t=session.beginTransaction();
       
   
       
//        Team team1=new Team();
//        team1.setTeamname("team1");
//       
//        Team team2=new Team();
//        team2.setTeamname("team2");
//       
//      
//      
//
//        //  Bag      ,        student
//        team1.getStudents().add("tom1");
//        team1.getStudents().add("tom1");
//        team2.getStudents().add("tom2");
//       
//       
//       
//        session.save(team1);
//        session.save(team2);
       

        Team t1=(Team)session.createQuery("from Team t where t.teamname='team1'").uniqueResult();
        t1.getStudents().remove(0);
        t.commit();
       
    }

}
 
実行結果:Hibernate:select team 0_.id as id0_, team0_.teamname as teamname0_ from teamIDBag team0_ where team0_.teamname='team1' Hibernate: select students0_.team_id as team1_0_, students0_.name as name0_, students0_.cid as cid0_ from studentIDBag students0_ where students0_.team_id=?
Hibernate: delete from studentIDBag where cid=? 赤い部分で、studentを削除するときはcidに基づいて削除できます.idbagを使用しないとdelete from studentIDBag where teamのようなものが出ます.id=?削除すべきでないデータを復元するには、insert操作に従います.