`

Hibernate实现类数少类信息多

阅读更多

问题描述:我在ITeye问答活动里看到一个题目,具体情况见http://www.iteye.com/problems/94079。

个人概括一下问题,应该是这样,

A,数据库有三张表,

Employees表结构(id,first_name,second_name,department_id,position_id)
Department_dict表结构(id,name)
Position_dict表结构(id,name)

B,在bean里面只有Employees对应的类,并没有Department_dict、Position_dict对应的类

C,用Hibernate 实现只有一个类的对象的情况下,将用户需要的name(用户全名first_name+second_name)、department_name、position_name都赋值到这个对象中

工具环境:Hibernate3.1.2,Mysql5.1,Win7(bit),tomcat6.0

解决过程:1.  beans里面新建Employees类

public   class  Employees  {
      private  int id;
      private  String first_name;
      private  String second_name;

      private  int department_id;

      private  int position_id;

 

      private  String name;

      private  String department_name;

      private  String position_name;


      public Employees(){ //设置默认值 }

 

      public  int getId()  {  return  id;  }
      public  void  setId(int Id)  {  this.id  =  Id;  }

      public  String getFirst_name()  {  return  first_name;  }
      public  void  setFirst_name(String First_name)  {  this.first_name  =  First_name;  }

      public  String getSecond_name()  {  return  second_name;  }
      public  void  setSecond_name(String Second_name)  {  this.second_name  =  Second_name;  }
      public  int getDepartment_id()  {  return  department_id;  }
      public  void  setDepartment_id(int Department_id)  {  this.department_id  =  Department_id;  }

      public  int getPosition_id()  {  return  position_id;  }
      public  void  setPosition_id(int Position_id)  {  this.position_id  =  Position_id;  }

 

      public  String getName()  {  return  name;  }
      public  void  setName(String Name)  {  this.name  =  Name;  }

      public  String getDepartment_name()  {  return  department_name;  }
      public  void  setDepartment_name(String Department_name)  {  this.department_name  =  Department_name;  }

      public  String getDepartment_name()  {  return  department_name;  }
      public  void  setDepartment_name(String Department_name)  {  this.department_name  =  Department_name;  }
 }

 

                 2.  配置Hibernate的cfg.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>
      <class  name ="domain.Employees"  table ="employees"  catalog ="employeelogs">
          <id  name ="id"  type ="java.lang.Integer">
              <column  name ="id"/>
              <generator  class ="native"/>
          </id>
          <property  name ="first_name"  type ="java.lang.String">
              <column  name ="first_name"/>
          </property>
          <property  name ="second_name"  type ="java.lang.String">
              <column  name ="second_name"/>
          </property>
          <property  name ="department_id"  type ="java.lang.String">
              <column  name ="department_id"/>
          </property>
          <property  name ="position_id"  type ="java.lang.String">
              <column  name ="position_id"/>
          </property>

          <property  generated ="insert"  name ="name"  formula ="(select concat(e.first_name,e.second_name) from Employees e where e.id = id)"/>
          <property  generated ="always"  name ="department_name"  formula ="(select d.name from Department_dict d where d.id = department_id)"/>
          <property  generated ="always"  name ="position_name"  formula ="(select p.name from position_dict p where p.id = position_id)"/>
      </class>
 </hibernate-mapping>

 

                 3.  Servlet里面的操作

...

Employees  employee  =   new  Employees();
employee.setId(33779);
employee.setFirst_name("Leo");
employee.setSecond_name("KunTing");
employee.setDepartment_id(213);
employee.setPosition_id(5578);
Transaction tran  =  session.beginTransaction();
session.save(employee);
tran.commit();

System.out.println(employee.getName());//全名
System.out.println(employee.getDepartment_name());//部门名称
System.out.println(employee.getPosition_name());//职位名称

...

看结果可知其实主要是发挥了Hibernate的Formula优势。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics