Hibernate中如果表中信息太多,可以讲表中信息分成多个部分也射到类中,这就是组件。
比如学生有身体状况和学习成绩两部分信息,下面例子是组件的使用方法:
Info.java:
/** * 学生身体状况信息 * * @author Caihanyuan * @time 2012-8-24 上午10:43:07 */ public class Info { private double height; private double weight; public Info() { } public Info(double height, double weight) { this.height = height; this.weight = weight; } //getter和setter方法 }
Report.java:
/** * 学生成绩信息 * * @author Caihanyuan * @time 2012-8-24 上午10:44:01 */ public class Report { private int math; private int chinese; private int english; public Report() { } public Report(int math, int chinese, int english) { super(); this.math = math; this.chinese = chinese; this.english = english; } //省略getter,setter }
Student.java:
/** * 学生实体类,对应于表格student * * @author Caihanyuan * @time 2012-8-24 上午10:42:53 */ public class Student { private String id; private String name; private Info info; private Report report; public Student() { } public Student(String id, String name, Info info, Report report) { this.id = id; this.name = name; this.info = info; this.report = report; } //getter,setter }
对应的配置文件:
Student.hbm.xml:
<hibernate-mapping> <class name="com.sunflower.pojo.Student" table="student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <!-- 身体状况组件信息Info --> <component name="info" class="com.sunflower.pojo.Info"> <property name="height" type="double"></property> <property name="weight" type="double"></property> </component> <!-- 成绩组件信息Report --> <component name="report" class="com.sunflower.pojo.Report"> <property name="math" type="int"></property> <property name="chinese" type="int"></property> <property name="english" type="int"></property> </component> </class> </hibernate-mapping>
反向生成表格:
drop table if exists student
create table student (id varchar(255) not null, name varchar(255), height double
precision, weight double precision, math integer, chinese integer, english integer,
primary key (id))
测试:
Test.java:
public class Test { public void save() { Session session = HibernateUtil.getSession(); Transaction ts = session.beginTransaction(); try { Info info = new Info(); info.setHeight(1.75); info.setWeight(65); Report report = new Report(); report.setMath(85); report.setChinese(90); report.setEnglish(95); Student studnet = new Student(); studnet.setName("柳长风"); studnet.setInfo(info); studnet.setReport(report); session.save(studnet); ts.commit(); } catch (Exception e) { e.printStackTrace(); ts.rollback(); } finally { HibernateUtil.closeSession(session); } } @SuppressWarnings("unchecked") public void get() { Session session = HibernateUtil.getSession(); Transaction ts = session.beginTransaction(); try { List list = session.createQuery("from Student").list(); Student student = (Student) list.get(0); System.out.println("name is:" + student.getName() + " height is:" + student.getInfo().getHeight() + " weight is:" + student.getInfo().getWeight()); ts.commit(); } catch (Exception e) { e.printStackTrace(); ts.rollback(); } finally { HibernateUtil.closeSession(session); } } public static void main(String[] args) { Test test = new Test(); test.save(); test.get(); } }
测试结果:
插入:
insert into student (name, height, weight, math, chinese, english, id) values
(?, ?, ?, ?, ?, ?, ?)
查询:
select student0_.id as id0_, student0_.name as name0_, student0_.height as
height0_, student0_.weight as weight0_, student0_.math as math0_, student0_.chinese as
chinese0_, student0_.english as english0_ from student student0_
name is:柳长风 height is:1.75 weight is:65.0
如果对组件进行双向映射,需要修改组件类和映射文件:
修改Info.java:
public class Info { private double height; private double weight; private Student student; public Info() { } public Info(double height, double weight) { this.height = height; this.weight = weight; } //getter和setter方法 }
Student.hbm.xml:
1 <!-- 身体状况组件信息Info --> 2 <component name="info" class="com.sunflower.pojo.Info"> 3 <parent name="student"/> 4 <property name="height" type="double"></property> 5 <property name="weight" type="double"></property> 6 </component>
在第3行中的中加入<parent/>标签就可以对组件进行反向求得整体.