Reverse Engineering of JPA Entities with JBoss Tools Eclipse Plugin.
The tutorial below details how to do reverse engineering of Hibernate/JPA POJO entities from database tables.
The database tables\entities which are used for this tutorial are Course, Department and Student. The way these 3 tables are related are Courses have a many-to-many relationship with Students and Students have a many-to-many relationship with Departments. This means we have 5 tables in all Student, Course, Department, Department_Student and Student_Course. The DDL for these 5 tables is given below...please take note of the foreign key relationships between tables as these will be interpreted as relationships in the reverse generated POJO entities...
[su_box title="Scripts for tables - Student, Department & Course" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java] CREATE TABLE `student` (
`STUDENT_ID` INT(11) NOT NULL,
`FIRST_NAME` VARCHAR(255) DEFAULT NULL,
`LAST_NAME` VARCHAR(255) DEFAULT NULL,
`AGE` INT(11) DEFAULT NULL,
`ADDRESS` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`STUDENT_ID`)
)
CREATE TABLE `department` (
`DEPT_ID` INT(11) NOT NULL,
`DEPT_NAME` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`DEPT_ID`)
)
CREATE TABLE `course` (
`COURSE_ID` INT(11) NOT NULL,
`COURSE_NAME` VARCHAR(255) NOT NULL,
`COURSE_FROM` TIME DEFAULT NULL,
`COURSE_TO` TIME DEFAULT NULL,
PRIMARY KEY (`COURSE_ID`)
)
CREATE TABLE `department_student` (
`Student_id` INT(11) NOT NULL,
`department_id` INT(11) NOT NULL,
PRIMARY KEY (`Student_id`,`department_id`),
KEY `FK_department` (`department_id`),
CONSTRAINT `FK_department` FOREIGN KEY (`department_id`) REFERENCES `department` (`DEPT_ID`),
CONSTRAINT `FK_student` FOREIGN KEY (`Student_id`) REFERENCES `student` (`STUDENT_ID`)
)
CREATE TABLE `student_course` (
`Student_id` INT(11) NOT NULL,
`Course_id` INT(11) NOT NULL,
PRIMARY KEY (`Student_id`,`Course_id`),
KEY `FK_course` (`Course_id`),
CONSTRAINT `FK_student_for_student_course` FOREIGN KEY (`Student_id`) REFERENCES `student` (`STUDENT_ID`),
CONSTRAINT `FK_course` FOREIGN KEY (`Course_id`) REFERENCES `course` (`COURSE_ID`)
)[/java][/su_box]
Note: The above DDL script is for MySQL database but should work fine for all relational databases. Also, I am using MySQL as the default database for all my tutorials.
Note 2: There are certain pre-requisites for reverse code generation. These are -
- JBoss Tools eclipse plugin(which includes Hibernate Tools) is needed for the reverse code generation. Please refer to the following tutorial to install JBoss Tools plugin in your Eclipse installation. How to Install JBoss Tools Plugin in Eclipse JunoClick to Read Article
- I am using an Eclipse JPA project for this tutorial created using Eclipse Juno. To know the steps for creating a JPA project from scratch along with a MySQL Data Connection using Eclipse Juno please refer to the following tutorial first Setup a JPA Project with MySQL Data ConnectionClick to Read Article