In our last article, we made our first model class, users, manually. However, creating tables manually is a repetitive and time-consuming task. So, in this article, we will use the Hibernate reverse engineering tool to generate some model classes for our example database, BookStoreDB. Below, you can see our entities. Here we are going to generate modes of category, book_order, book, order_detail, review, and customer.
In our last article, we made an eclipse project, BookStoreProject. To install the Hibernate reverse engineering tool head over to Help > Eclipse MarketPlace > Search JBoss tool > select only hibernate tools from the package.
To generate model classes from tables in the database we need to create Hibernate configuration.
Create a new hibernate configuration file named hibernate.cfg.xml, which we can put under the source folder. Open the Hibernate configuration file (cfg.xml) window and fill in the details, or you can do it manually like below:
1 2 3 4 5 6 7 8 9 10 11 12 13
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">MySQL@1234</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstoredb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration>
The above values need to be accurate because they establish your connection to the DB.
To get started, click on the add button, which you can see below. Upon opening, you will see:
As we have already configured our project figure, you can see some fields are filled. Now, let’s go through this one by one.
You can see the Hibernate version of our application is 5.2.
In the project section, we can select our project, i.e, BookStoreProject.
Next, we can provide the path to our configuration file hibernate.cfg.xml.
Lastly, select the annotation type so Hibernate will generate model classes with annotation to map the model classes to tables in the database.
Click ok, and now you can see your new configuration.
All the tables and databases are in our system, meaning everything is good.
Just for reference, I am attaching the pom.xml file which is the main file handling all the dependencies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bookstore</groupId> <artifactId>BookStoreWebsite</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>test</testSourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>14</release> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.31.Final</version> <scope></scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>5.5.6.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> <scope>runtime</scope> </dependency> </dependencies> </project>
To generate code for the model class go to the menu and click Hibernate Code Generation Configurations or click menu Run > Hibernate Code Generation > Hibernate Code Generation Configurations and create a new configuration. Our configuration looks like this:
Now click on run, and Hibernate will automatically create our model classes for us. Let’s look at one of them to see the similarities to our table in the database.
Generated Rating.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.bookstore.entity;
// Generated 05-Jul-2021, 12:09:47 pm by Hibernate Tools 5.2.12.Final
/**
* Review generated by hbm2java
*/
@Entity
@Table(name = "review", catalog = "bookstoredb")
public class Review implements java.io.Serializable {
private int reviewId;
private Customer customer;
private Book book;
private int rating;
private String headline;
private String comment;
private Date reviewTime;
@Id
@Column(name = "review_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getReviewId() {
return this.reviewId;
}
public void setReviewId(int reviewId) {
this.reviewId = reviewId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "customer_id", nullable = false)
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "book_id", nullable = false)
public Book getBook() {
return this.book;
}
public void setBook(Book book) {
this.book = book;
}
@Column(name = "rating", nullable = false)
public int getRating() {
return this.rating;
}
public void setRating(int rating) {
this.rating = rating;
}
@Column(name = "headline", nullable = false, length = 128)
public String getHeadline() {
return this.headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
@Column(name = "comment", nullable = false, length = 500)
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "review_time", nullable = false, length = 19)
public Date getReviewTime() {
return this.reviewTime;
}
public void setReviewTime(Date reviewTime) {
this.reviewTime = reviewTime;
}
}
// Generated 05-Jul-2021, 12:09:47 pm by Hibernate Tools 5.2.12.Final
/**
* Review generated by hbm2java
*/
The message above displays the version of Hibernate tools used and the date and time of the generation of the file.
Now if we try and compare the similarities of our file to the table, we can see different annotations used. Let’s learn about them a little:
@Entity: Annotation to specify that this class is mapped to the table in the database with the same name.
@Column(name = “review_id”): Specifies the actual column name of the field in the database. It is only used when the column name differs from that of the class and that too before the getter method of a field.
@Id: Defines the primary key of the table.
@GeneratedValue(strategy = GenerationType.IDENTITY ): Used before the getter method of the field that mapped to the primary key column in the database.
@ManyToOne(fetch = FetchType.EAGER): When many instances of this entity are mapped to one instance of another entity.
@JoinColumn(name = “customer_id”, nullable = false): When we want to create multiple join columns we can use the @JoinColumns annotation.
@Temporal (TemporalType.TIMESTAMP): Converts back and forth between timestamp and java.util.Date. It also converts time-stamp into time.
That’s all for this article. Thank you for reading!