Hibernatetools implementation of Pojo class database Schma mapping mapping of mutual transformation two

Source: Internet
Author: User

Next blog: hibernatetools implementation of Pojo class database Schma mapping mapping of mutual transformation


idea two: By the database table, generate mapping mapping file and Pojo class.

Although it can be achieved, the individual feels that designing the database first, and then generating classes that do not conform to Hibernate's face-to-object persistence mode of thinking. All right. Let's just say the steps. First, two tables were created in the test database, course and teacher tables.

--------------------------------table structure for course------------------------------DROP table IF EXISTS ' Course ' ; CREATE TABLE ' Course ' (  ' id ' int (one) not null,  ' name ' varchar (255) DEFAULT NULL,  PRIMARY KEY (' id ')) engine=in Nodb DEFAULT Charset=utf8;--------------------------------Table structure for teacher------------------------------ DROP TABLE IF EXISTS ' teacher '; CREATE TABLE ' teacher ' (  ' id ' int (one) not null,  ' name ' varchar (255) DEFAULT NULL,  PRIMARY KEY (' id ')) engine=i Nnodb DEFAULT Charset=utf8;
After you've built the table, right--->new on the Eclipse project, for example, select the third item in the box, which is used to configure the database table that you chose to generate the Pojo class for. reveng.xml file.


Select the console configuration item that you created in the previous blog post. Click Refresh under the Database schema box. After you can see the test database, click on the course and teacher tables, select the include after selecting all, then click Finish, for example


Then go to hibernate Code Generation Configuration window, first configure the Output Directory Export folder, in the next check box tick, and then in the package column to write the file to be output to which packet, and select the Reveng.xml file that you just configured


Configure the items to output, where the first two items are selected. Build. Java and. Hbm.xml, which is the Pojo class and mapping mapping file we want. Then click Run to be good.


The results are for example:


Code for the generated mapping mapping file

<? 

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 "> <colum n name= "name"/> </property> </class>

<?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& Gt <property name= "Name" type= "string" > <column name= "name"/> </property> <set Name= "Courses" table= "Course" inverse= "true" lazy= "true" fetch= "select" > <key> <col Umn name= "Teacherid" not-null= "true"/> </key> <one-to-many class= "Org.hibernate.test.Co Urse "/> </set> </class>


The generated Pojo class:

Package org.hibernate.test;//Generated 2014-5-31 11:19:19 by Hibernate Tools 4.0.0/** * Course Generated by Hbm2java */pu Blic class Course implements java.io.Serializable {private int id;private Teacher teacher;private String name;public Cours E () {}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;p Rivate String name;private Set courses = new HashSet (0);p ublic 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;}}

We're done here. The process of generating Pojo class and mapping mapping files from database tables


idea three: Generate Database DDL and Pojo classes from mapping mapping files


First, create a new mapping file. This establishes the Department.hbm.xml in the project.

<?

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 ">


Next build a new console configuration file, the basic configuration and the process configured above, the most critical is to increase the mapping file.


Next. Change Hibernate Code Generation configuration, preferred to select the newly configured console configuration file


Next, select the schema and. java files that you want to generate, and run will be able to do so.


Finally the result


The generated DDL code is

CREATE table DEPARTMENT (ID integer NOT NULL, NAME varchar (255), primary key (ID));</span>

Pojo class:

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 () {}publi C 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;}}

All right. At this point the Pojo class, the mapping file and the database table are converted to each other. Of course this is achieved with the help of Eclipse plug-ins, and friends who are familiar with ant can also use ant script to implement, detailed tutorial to Google Bar.

The official tutorials for Hibernatetools are recommended here. Both the Eclipse plugin and the ant script are implemented in a comprehensive way.



Hibernatetools implementation of Pojo class database Schma mapping mapping of mutual transformation two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.