Hibernate的关系映射-------多对一与一对多的实现举例

Hibernate的关系映射-------多对一与一对多的实现举例

此处以部门(Department)与部门员工(Employee)为例,显而易见department为一的一方,员工为多的一方。

 

Domain层的创建:

 

Department类的实例化:

public class Department {

    private Integer id;

    private String name;

     privateSet<Employee> emps;

     

    publicSet<Employee> getEmps() {

       return emps;

    }

    public voidsetEmps(Set<Employee> emps) {

       this.e配置文件的编写代码:

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/HibernateConfiguration DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

    <session-factory name="foo">

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="connection.url">jdbc:mysql:///demo</property>

       <property name="hibernate.connection.username">root</property>

       <property name="connection.password">123</property>

       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

       <property name="hibernate.hbm2ddl.auto">update</property>

       <property name="hibernate.show_sql">true</property>

      

       <mapping resource="com/hbsi/domain/Department.hbm.xml"/>

       <mapping resource="com/hbsi/domain/Employee.hbm.xml"/>  </session-factory>

</hibernate-configuration>

各个包的导入:

mps = emps;

    }

    public Integer getId() {

       return id;

    }

    public void setId(Integer id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(Stringname) {

       this.name = name;

    }

   

}

     Department映射文件的创建:

       <?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping package="com.hbsi.domain">

 

    <class name="Department" table="department">

       <id name="id">

           <generator class="native"/>

       </id>

       <property name="name"/>

       <set name="emps">

           <key column="depart_id"/><!-- 查找部门下所包含的员工,根据主外键查 -->

           <one-to-many class="Employee"/>   

       </set>

    </class>

</hibernate-mapping>

Employee的实例化:

public class Employee {

    private Integer id;

    private String name;

    private Department depart;

   

    public Integer getId() {

       return id;

    }

    public void setId(Integer id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(Stringname) {

       this.name = name;

    }

    public DepartmentgetDepart() {

       return depart;

    }

    public voidsetDepart(Department depart) {

       this.depart = depart;

    }

   

}

Employee映射文件的创建:

              <?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping package="com.hbsi.domain">

 

    <class name="Employee" table="employee">

       <id name="id">

           <generator class="native"/>

       </id>

       <property name="name"/>

       <many-to-one name="depart"column="depart_id"/>    此处多对一的列必须要与上面set标签中key中coulmn中的相同

    </class>

</hibernate-mapping>

配置文件的编写代码:

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/HibernateConfiguration DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

    <session-factory name="foo">

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="connection.url">jdbc:mysql:///demo</property>

       <property name="hibernate.connection.username">root</property>

       <property name="connection.password">123</property>

       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

       <property name="hibernate.hbm2ddl.auto">update</property>

       <property name="hibernate.show_sql">true</property>

      

       <mapping resource="com/hbsi/domain/Department.hbm.xml"/>

       <mapping resource="com/hbsi/domain/Employee.hbm.xml"/>  </session-factory>

</hibernate-configuration>

各个包的导入:

 

获取Session的工具类Util的编写:

public final class HibernateUtil {

    private static SessionFactory sessionFactory;

    private HibernateUtil(){

      

    }

    static{

       Configuration cfg=new Configuration();

       cfg.configure();

       sessionFactory=cfg.buildSessionFactory();

    }

    public static SessionFactory getSessionFactory() {

       return sessionFactory;

    }

    public static Session getSession(){

       return sessionFactory.openSession();

    }

   

}

 

此处以插入方法实现一对多的测试类的编写:

 

       //插入方法的实现

    static Department add(){

       Session session=null;

       Transaction tx=null;

       try{

           session=HibernateUtil.getSession();

           tx=session.beginTransaction();

           //添加

           Department dep=new Department();

           dep.setName("departone");

          

           Employee e1=new Employee();

           e1.setName("Tom");

           e1.setDepart(dep);//指定员工在哪个部门-----对象模型,对象建立关联关系          

          

           session.save(dep);

           session.save(e1);

          

           tx.commit();

          

           return dep;

       }finally{

           if(session!=null){

              session.close();

           }

       }

      

    }

 

 

主函数的调用:

    public static void main(String[] args) {

       // TODO Auto-generatedmethod stub

       add();

    }

 

然后以查找的形式实现的多对一测试类的编写:

   static Employee query(int empId){

       Session session=null;

       try{

           session=HibernateUtil.getSession();

           //查询的操作

           Employee e=(Employee) session.get(Employee.class,empId);

           System.out.println(e.getName()+" "+e.getDepart().getName());

           return e;

       }finally{

           if(session!=null){

              session.close();

           }

       }

    }

主函数的调用:

       public static void main(String[] args){

       query(1);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值