关联映射----多对一小实例

 

关联映射----多对一小实例

Department.java

public class Department {

    private int id;

    private String name;

    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;

    }

}

 

Department.hbm.xml

<?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" column="id">

       <!-- native自动增长   根据不同的底层数据库选择产生不同的策略 -->

           <generator class="native"/>

       </id>

       <property name="name" column="name"/>

    </class>

</hibernate-mapping>

 

Employee.java

public class Employee {

    private int id;

    private String name;

    //站在员工的角度(多对一)

    //如果写private int departId;,在通过id查询时只能得到部门id要想得到部门的名称还要手工的通过之前获取到的部门id去查

    private Department depart;

   

    public Department getDepart() {

       return depart;

    }

    public void setDepart(Department depart) {

       this.depart = depart;

    }

    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;

    }

}

 

Employee.hbm.xml

<?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" column="id">

       <!-- native自动增长   根据不同的底层数据库选择产生不同的策略 -->

           <generator class="native"/>

       </id>

       <property name="name"/>

       <!-- 映射文件的属性就不能用property -->

       <!-- 用many-to-one实现多对一的映射 -->

       <!-- 如果不带foreign-key属性,它会默认使用depart ,如果带上这个属性,可以知道另一张表 -->

       <many-to-one name="depart" column="depart_id"/>

    </class>

</hibernate-mapping>

 

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

<hibernate-configuration>

    <session-factory>

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

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

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

       <property name="hibernate.connection.password">1234</property>

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

       <!-- hbm2ddl的四种状态:create、update、create_drop、validate -->

       <!-- create:在程序运行时生成表格,在程序结束时不会销毁表格,在下次运行时销毁重建 -->

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

      

       <!-- 显示产生的sql语句 -->

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

      

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

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

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

    </session-factory>

</hibernate-configuration>

 

ManyToOne.java

package com.hbsi.test;

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.hbsi.domain.Department;

import com.hbsi.domain.Employee;

import com.hbsi.hibernate.utils.HibernateUtil;

public class ManyToOne {

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       add();

       query(1);

       //Employee e=query(1);

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

      

    }

   

    //能否正确插入,查询

    //添加相应的信息

    static Department add(){

       Session s=null;

       Transaction tx=null;

       try{

           s=HibernateUtil.getSession();

           tx=s.beginTransaction();

          

           //增加部门,员工

           Department dep=new Department();

           dep.setName("部门一");

          

           Employee e1=new Employee();

           e1.setName("Tom");

           e1.setDepart(dep);//对象模型,对象建立关联关系

          

           s.save(dep);

           s.save(e1);

          

           tx.commit();

          

           return dep;

       }finally{

           if(s!=null){

              s.close();

           }

       }

    }

    //根据员工的id去查员工的信息,并查出部门的名字

    static Employee query(int empId){

       Session s=null

       try{

           //查询

           s=HibernateUtil.getSession();

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

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

       //  Hibernate.initialize(e.getDepart());//可以初始化代理对象,在主方法中输出就不会报错了

           return e;

       }finally{

           if(s!=null){

              s.close();

           }

       }

    }

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值