12 집합 맵 맵 집합

5992 단어 map
   Map    
Map              

           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="Employee">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" unique="true"/>
		<many-to-one name="derpartment" column="depaer_id" not-null="true"/>
	</class>
	
</hibernate-mapping>

unique            

     

package vo.util.bean;
/**
 *    
 * @author Administrator
 *
 */
public class Employee {
    private int id;
    private String name;
    private Department derpartment;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Department getDerpartment() {
		return derpartment;
	}
	public void setDerpartment(Department derpartment) {
		this.derpartment = derpartment;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "id="+this.id+" name="+this.name;
	}
	
}


           
package vo.util.bean;

import java.util.Map;


/**
 *    
 * @author Administrator
 *
 */
public class Department {
  private int id;
  private String name;
  private Map<String,Employee> ems;
  
public Map<String, Employee> getEms() {
	return ems;
}

public void setEms(Map<String, Employee> ems) {
	this.ems = ems;
}

public int getId() {
	return id;
}

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



     Map    
private Map<String,Employee> ems;


     
<?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"/>
		<!--             -->
		<!-- 
		<set name="ems">
		 -->
		<!--          -->
		<!--  
		<key column="depaer_id"/>
		-->
		   <!--            -->
		   <!-- 
		  <one-to-many  class="Employee" />
		</set>
		
		 -->
        <map name="ems">
        <key column="depaer_id"/>
		 <map-key type="string" column="name"/>
		 <one-to-many class="Employee"/>
		 </map>
	</class>
	
</hibernate-mapping>

       List           map                
 <map-key                     Map            
type             string     column       ?     
                    name          

   

package vo.util.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

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);
    		
    		Map<String,Employee> ems=new HashMap<String, Employee>();
    	    ems.put(em1.getName(), em1);
    	    ems.put(em2.getName(), 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);
    	   
    	   System.out.println(d.getEms());
    	  
    		return d;
    	}finally{
    		if(session != null)session.close();
    	} 

    }
}


      :             

end   !

좋은 웹페이지 즐겨찾기