Next blog: HibernateTools implements mutual conversion of pojo-class database schma mapping
Idea 2: The Mapping File and POJO class are generated from the database table.
Although it can be implemented, I personally think that designing a database first and then generating a class does not conform to Hibernate's way of thinking about Object persistence. Well, let's talk about the steps. First, create two tables in the test Database: The course table and the teacher table.
-- ------------------------------ 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;
After the table is created, right-click the eclipse project --> new, for example, the third item in the selection box. The reveng. xml file is used to configure and select the database table to generate the POJO class.
Select the Console configuration item created in the previous blog, click refresh in the Database schema box, and then you can see the test Database. Click it to display the course and teacher tables. Select All and click Include, click finish, as shown in figure <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090250138.png" alt = "\">
Next, go to the Hibernate Code Generation Configuration form. First, configure the Output directory of the Output directory, check the check box next to each other, and then write the package in the package column the package to which the generated file will be Output, and select the configured reveng. xml file
Configure the items to be output. Select the first two items to generate. java and. hbm. xml, which is the desired POJO class and Mapping File. Then click run.
The result is as follows:
Code of the ing file generated
Generated POJO class:
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;}}
At this point, we have completed the process of generating POJO class and Mapping Files from database tables.
Idea 3: Generate database DDL and POJO classes by Mapping Files
First, create a new Mapping file. Here, we create Department. hbm. xml in the project.
Next, create a new Console Configuration file. The basic Configuration is the same as the above Configuration process. The most important thing is to add the mapping File.
Next, change the Hibernate Code Generation Configuration. The Console Configuration file with the new Configuration is preferred.
Next, select the Schema and. Java file to be generated, and run it.
Final Result
The generated DDL code is
create table DEPARTMENT (ID integer not null, NAME varchar(255), primary key (ID));
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() {}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;}}
Now, the conversion between the Mapping file and the database table in the POJO class is complete. Of course, this is achieved by using the eclipse plug-in. Friends who are familiar with ant can also implement it by using the ant script, go to google for the specific tutorial. Here we recommend the official tutorial of HibernateTools, which includes two implementation methods: eclipse plug-in and ant script, which are comprehensive.