十三array配列形式のマッピング

3681 ワード

 List , bag 
 
package vo.util.bean;

import java.util.Set;

/**
 *  
 * @author Administrator
 *
 */
public class Department {
  private int id;
  private String name;
  private Employee[] ems;
  
public int getId() {
	return id;
}

public Employee[] getEms() {
	return ems;
}

public void setEms(Employee[] ems) {
	this.ems = ems;
}

public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
}

  
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="vo.util.bean">

	<class name="Department">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		
		<array name="ems">
		<key column="depaer_id"/>
		<list-index column="ol"/>
		<one-to-many class="Employee"/>
		</array>
	</class>
	
</hibernate-mapping>

 <key column="depaer_id"/>
		<list-index column="ol"/>
		<one-to-many class="Employee"/>
		 List   

 <array> 

 
package vo.util.test;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import vo.util.HibernateUtil;
import vo.util.bean.Department;
import vo.util.bean.Employee;

public class OneToMany {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	
         add();
        query(1);
	}
    static Department add(){
    	Session session = null;
    	Transaction tx = null;
    	try{
    		session =HibernateUtil.getSession();
    		tx = session.beginTransaction();
    		//… save,delete,update,get…
    		Department dpart=new Department();
    		dpart.setName(" ");
    		
    		Employee em1=new Employee();
    		em1.setName(" ");
    		em1.setDerpartment(dpart);
    		
    		Employee em2=new Employee();
    		em2.setName(" ");
    		em2.setDerpartment(dpart);
    		
    		Employee[] ems=new Employee[]{em1,em2};
    	    
    	    dpart.setEms(ems);
    		session.save(dpart);
    		session.save(em1);
    		session.save(em2);
    		
    		
    		tx.commit();
    		return dpart;
    	}finally{
    		if(session != null)session.close();
    	} 

    }
    
    static Department query(int id){
    	Session session=null;
    	
    	try{
    	   session=HibernateUtil.getSession();
    	   Department d=(Department)session.get(Department.class, id);
    	   Employee[] a=d.getEms();
    	   for(int i=0;i<a.length;i++){
    		   System.out.println(a[i]);
    	   }
    	   
    	  
    		return d;
    	}finally{
    		if(session != null)session.close();
    	} 

    }
}

  
end  !