Hibernate多对一双向关联(xml配置)_hibernate


Department.java 
package  com.many2one.bean;

import  java.util.Set;

public   class  Department {
    
     private   int  id;
     private  String name;
     private  Set < Employee >  sets;
     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  Set < Employee >  getSets() {
         return  sets;
    }
     public   void  setSets(Set < Employee >  sets) {
         this .sets  =  sets;
    }
    
}
Employee.java 
package  com.many2one.bean;


public   class  Employee {
    
     private   int  id;
     private  String name;
     private  Department department;
     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 getDepartment() {
         return  department;
    }
     public   void  setDepartment(Department department) {
         this .department  =  department;
    }
    
    
}
映射文件 
Department.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" > 
     < hibernate-mapping  package ="com.many2one.bean" > 
         < class  name ="Department"  table ="department"  catalog ="Hibernate_Many2One" > 
             < id  name ="id"  column ="id" > 
                 < generator  class ="native" /> 
             </ id > 
             < property  name ="name"  column ="name" /> 
            
         </ class > 
     </ hibernate-mapping >
Employee.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" > 
     < hibernate-mapping  package ="com.many2one.bean" > 
         < class  name ="Employee"  table ="employee"  catalog ="Hibernate_Many2One" > 
             < id  name ="id"  column ="id" > 
                 < generator  class ="native" /> 
             </ id > 
             < property  name ="name"  column ="name" /> 
             < many-to-one  name ="department"  column ="deptid"  cascade ="all"   /> 
         </ class > 
     </ hibernate-mapping >
导出的sql 
create   table  Hibernate_Many2One.department (
        id  integer   not   null  auto_increment,
        name  varchar ( 255 ),
         primary   key  (id)
    )

     create   table  Hibernate_Many2One.employee (
        id  integer   not   null  auto_increment,
        name  varchar ( 255 ),
        deptid  integer ,
         primary   key  (id)
    )

     alter   table  Hibernate_Many2One.employee 
         add   index  FK4722E6AE3829E242 (deptid), 
         add   constraint  FK4722E6AE3829E242 
         foreign   key  (deptid) 
         references  Hibernate_Many2One.department (id)

测试代码 
@Test
     public   void  insert(){
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = session.beginTransaction();
         try  {
            transaction.begin();
            Department department = new  Department();
            department.setName( " 软件测试部 " );
            Employee employee = new  Employee();
            employee.setName( " 张三 " );
            employee.setDepartment(department);
            session.save(employee);
            transaction.commit();
        }  catch  (HibernateException e) {
            e.printStackTrace();
        }
    }
    
    
    @Test
     public   void  select(){
        Session session = HibernateSessionFactory.getSession();
         try  {
            Employee employee = (Employee) session.get(Employee. class ,  1 );
            System.out.println(employee.getName());
            System.out.println(employee.getDepartment().getName());
        }  catch  (HibernateException e) {
            e.printStackTrace();
        }
    }