接着上一篇博客:HibernateTools实现pojo类 数据库schma mapping映射的相互转换
虽然可以实现,但个人觉着先设计数据库,然后再生成类不符合Hibernate的面对对象持久化的思维方式。好了,还是说步骤吧,首先在test数据库建立两张表,分别为course表和teacher表
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
建好表后,在eclipse项目上右键-->new,如下图,选择框中的第三项,这个reveng.xml文件用于配置 选择要生成POJO类的数据库表。
选择上篇博客中创建的Console configuration项,点Database schema框下的refresh,之后可以看到test数据库,单击就出现了course和teacher表,全选后点击Include,之后点finish,如下图
再来到Hibernate Code Generation Configuration窗体,首先配置下Output directory输出目录,在尽挨着的复选框打上勾,然后在package栏写上生成文件要输出到哪个包,并选择刚配置好的reveng.xml文件
配置要输出的项,这里选定前两项,生成.java和.hbm.xml,就是我们想要的POJO类和Mapping映射文件。之后点击run就好了。
结果如下图:
生成的Mapping映射文件的代码
package org.hibernate.test;
// Generated 2014-5-31 11:19:19 by Hibernate Tools 4.0.0
/**
* Course generated by hbm2java
*/
public class Course implements java.io.Serializable {
private int id;
private Teacher teacher;
private String name;
public Course() {
}
public Course(int id, Teacher teacher) {
this.id = id;
this.teacher = teacher;
}
public Course(int id, Teacher teacher, String name) {
this.id = id;
this.teacher = teacher;
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Teacher getTeacher() {
return this.teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
package org.hibernate.test;
// Generated 2014-5-31 11:19:19 by Hibernate Tools 4.0.0
import java.util.HashSet;
import java.util.Set;
/**
* Teacher generated by hbm2java
*/
public class Teacher implements java.io.Serializable {
private int id;
private String name;
private Set courses = new HashSet(0);
public Teacher() {
}
public Teacher(int id) {
this.id = id;
}
public Teacher(int id, String name, Set courses) {
this.id = id;
this.name = name;
this.courses = courses;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourses() {
return this.courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
}
首先,新建一个Mapping文件,这里在项目中建立Department.hbm.xml。
接下来,改下Hibernate Code Generation Configuration就好了,首选选择新配置的Console Configuration文件
接下来选择要生成的Schema和.Java文件,然后run就可以了。
最终结果如图:
生成的DDL代码为
create table DEPARTMENT (ID integer not null, NAME varchar(255), primary key (ID));
package org.hibernate.test;
// Generated 2014-5-31 16:23:27 by Hibernate Tools 4.0.0
/**
* Department generated by hbm2java
*/
public class Department implements java.io.Serializable {
private int id;
private String name;
public Department() {
}
public Department(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}