【HibernateTools实现pojo类 数据库schma mapping映射的相互转换 二】幽沉谢世事,俯默窥唐虞。这篇文章主要讲述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.0import 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脚本两种实现方式,很全面。
推荐阅读
- 关于安卓浏览器无法识别es6语法
- 一种Android数据请求框架
- android项目中的拍照和本地图片截图
- Android Api Component---翻译任务和回退栈(Tasks and Back Stack)
- Android中线程间通信原理分析(Looper,MessageQueue,Handler)
- Java +安卓 定时任务
- Android攻城狮http协议
- android studio的git笔记
- 关于Android SDK Manager更新速度慢的解决方法