HibernateTools实现pojo类 数据库schma mapping映射的相互转换 二

接着上一篇博客:HibernateTools实现pojo类 数据库schma mapping映射的相互转换


思路二:由数据库表,生成Mapping映射文件和POJO类。

   虽然可以实现,但个人觉着先设计数据库,然后再生成类不符合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映射文件的代码

<?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">
<!-- Generated 2014-5-31 11:19:19 by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
    <class name="org.hibernate.test.Course" table="course" catalog="test">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="teacher" class="org.hibernate.test.Teacher" fetch="select">
            <column name="teacherId" not-null="true" />
        </many-to-one>
        <property name="name" type="string">
            <column name="name" />
        </property>
    </class>
</hibernate-mapping>
<?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">
<!-- Generated 2014-5-31 11:19:19 by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
    <class name="org.hibernate.test.Teacher" table="teacher" catalog="test">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
        <set name="courses" table="course" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="teacherId" not-null="true" />
            </key>
            <one-to-many class="org.hibernate.test.Course" />
        </set>
    </class>
</hibernate-mapping>

生成的POJO类:

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;
	}

}

到此我们就完成了由数据库表生成POJO类和Mapping映射文件的过程


思路三:由Mapping映射文件生成数据库DDL和POJO类


首先,新建一个Mapping文件,这里在项目中建立Department.hbm.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="org.hibernate.test.Department" table="DEPARTMENT">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="increment"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
    </class>
</hibernate-mapping>

接下来建一个新的Console Configuration文件,基本配置和上文中配置的过程一样,最关键的是加入mapping文件。


接下来,改下Hibernate Code Generation Configuration就好了,首选选择新配置的Console Configuration文件


接下来选择要生成的Schema和.Java文件,然后run就可以了。


最终结果如图:


生成的DDL代码为

create table DEPARTMENT (ID integer not null, NAME varchar(255), primary key (ID));</span>

POJO类:

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;
	}

}

    好了,至此POJO类,Mapping文件和数据库表相互转化就都介绍完了,当然这是借助eclipse的插件实现的,熟悉使用ant的朋友也可以借助ant脚本来实现,具体的教程去google吧。这里推荐下HibernateTools的官方教程,包含了eclipse插件和ant脚本两种实现方式,非常全面。



HibernateTools实现pojo类 数据库schma mapping映射的相互转换 二,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。