HibernateTools實現pojo類 資料庫schma mapping映射的相互轉換 二

來源:互聯網
上載者:User

標籤:tput   sch   his   轉換   href   ppi   efault   import   net   

接著上一篇部落格: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指令碼兩種實現方式,很全面。



HibernateTools實現pojo類 資料庫schma mapping映射的相互轉換 二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.