问题描述:我在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优势。
相关推荐
此EntityDao接口与EntityDaoImpl实现类,可取代了我们平时写的UserDao,NewsDao,CompanyDao等等,可直接供Service层调用
struts2 hibernate 实现无限级联 树形分类 基于struts2 hibernate 技术,实现对无限级联分类的crud操作
hibernate结构框架及组要实现类的深入分析 用来改造hiberna,实现sdo das
java 实现的一个简单的hibernate分页类 可以设置,从某一条开始取、显示的条数 不依赖struts spring
Spring+Hibernate多数据源的整合实现demo
使用Hibernate 包装的分页类. 应用于项目开发,实现数据的分页.
使用Hibernate实现增删改查,将我们需要的dialect.class的值复制粘贴到我们自己建的文件中 url,username,password 的值就用之前我们讲过的即可; 配置文件中的 <property name="show_sql">true</property> 是为了在...
User实体(id, name, age, sex, ctime),使用hibernate实现User的增删改查。要求: 1、包的层次: com.xxx.entity, com.xxx.dao, com.xxx.dao.impl.hql, com.xxx.dao.impl.qbc, com.xxx.dao.impl.sql, ...
通过使用Strust2+Hibernate实现简单CRUD基本操作,有需要可以下载学习。
hibernate_mysql_struts2 实现的通用分页类.欢迎指正
分页查询就是把数据库中某张表的记录数进行分页查询,在做分页查询时会有一个Page类,下面是一个Page类,我对其做了详细的注解:。。。。。。。。。。。。
后台的查询功能单独用类StudentDao封装(与Action类脱离),StudentDao使用Spring创建,StudentDao中支持参数flag,0时直接使用静态数据查询,1时使用hibernate查询数据库,flag值采用Spring方式赋值(在xml中配置)
12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java ...
01. 采用后台及前台的 Spring + Spring mvc + Hibernate + Bootstrap 02. 后台全注解式的开发(除了必要的spring和hibernate的xml配置以外) 03. 后台通过自定义注解结合一个访问拦截器实现整个系统的权限控制 04...
12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java ...
HIbernate购物车例子,实现增删改查,包括列表展示 添加 删除 和修改。源代码和工具类。比较详细,一看就懂hibernate.
已经建立好了数据库对应的实体类,打开之后直接在项目中练习查询就行。Hibernate和JPA方式都有了。 项目需求: 1. jdk8(因为用到了lambda表达式)。 2. Eclipse或者IDEA开发环境,推荐IDEA,因为我就是用IDEA做的...
基于hibernate的分页开发技术,分页的实体类和分页的辅助类完成分页功能。
这个类完全可以实现对数据库的基本操作;此类适用于hibernate4以上版本 ;hibernate4用hibernateSupport很容易引发一系列的问题,如果想避免的话,可以用此类
代码清晰容易接受,如果忘记了可以拿出来参考哦