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 implemented, the individual feels that designing the database first and then generating classes that do not conform to Hibernate's face-to-object persistence mindset. Well, let's just say the steps, first build two tables in the test database, course tables and teacher tables, respectively.
--------------------------------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 have built the table, right--->new on the Eclipse project, such as the third item in the Select box, which is used to configure the database table that selects the Pojo class to be generated.
Select the console configuration item that you created in the previous blog, click Refresh under the Database schema box, and then you can see the test databases, clicking on the course and teacher tables, clicking on the include after selecting all, Then click Finish, as
Then go to hibernate Code Generation configuration form, first configure the output directory to print directories, tick the check box next to each other, 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, and generate. java and. Hbm.xml, which is the Pojo class and mapping mapping file we want. Then click Run to be good.
Results such as:
code for the generated mapping map 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;}}
Here we complete the process of generating the Pojo class and the mapping mapping file from the database table
idea three: Generate Database DDL and Pojo classes from mapping mapping files
First, create a new mapping file, where the Department.hbm.xml is established 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 join the mapping file.
Next, change the hibernate Code Generation configuration, preferred to select the new configured console configuration file
Next, select the schema and. java file that you want to generate, and then run it.
Final 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;}}
Well, so far Pojo class, mapping file and database tables are all introduced to each other, of course, this is done with the help of Eclipse plug-ins, familiar with the use of ant friends can also be implemented with the help of Ant script, the specific tutorial to Google Bar. An official tutorial on Hibernatetools is recommended here, which includes both the Eclipse plugin and ant script implementations, all of which are very comprehensive.