Java and PostgreSQL Web Application PART 4

Introduction

This tutorial is the fourth part in the series explaining how to create and configure the MVC controller class of the Java and PostgreSQL Web Application from scratch with ECLIPSE, Spring Data, Tomcat and other databases. MVC controllers are designed to respond to requests made to a ASP.NET MVC website where each browser request is represented with a specific controller.

Prerequisites

  • A working knowledge and understanding of the codes presented in previous three sections of this tutorial series.

How to Configure Persistence in Eclipse IDE

This section will explain how to set up database connection properties using a JPA configuration file in the form of an XML file.

First, create a folder under the src directory. Right-click the source directory folder then select the new folder. Name the folder “META-INF” and then click “Finish.” The following screenshot provides an example:

image shows the create folder window

Next, create a new XML file within the META-INF folder by right-clicking the META-INF folder. Next, select “New” and then locate the XML file option to open up the New XML File window. The new XML file is persistence.xml. Now click “Finish” to create the file. The following screenshot provides an example:

image shows the window for new xml file

With the persistence file created, configure the file by copying an existing example of a JPA persistence 2.1 XML file. Do this by going to this URL: Sample JPA 2.1 persistence units.

Here copy only the root element “persistence,” as shown in the following image:

image show the root persistence

Now paste the copied root element inside the persist.xml file. The complete configuration of the XML file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">

<persistence-unit name="CarsDB">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/cars">
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver">
<property name="javax.persistence.jdbc.user" value="postgres">
<property name="javax.persistence.jdbc.password" value="1234">

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect">
<property name="hibernate.hbm2ddl.auto" value="update">

</properties>
</persistence-unit>

</persistence>

How to Configure Persistence in Eclipse IDE

  • Following is a step-by-step explanation of the above configuration, with the below properties block describing the database connection:
  1. The URL of the PostgreSQL database is defined.
  2. The JDBC driver for the PostgreSQL database is indicated.
  3. The PostgreSQL user is specified.
  4. The user password is provided.
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/cars">
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver">
<property name="javax.persistence.jdbc.user" value="postgres">
<property name="javax.persistence.jdbc.password" value="1234">
  • The persistence is now configured to use an SQL dialect for the PostgreSQL database as follows:
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect">
  • Use the hbd2ddl.auto property of “hibernate.” This allows hibernate to check for tables and columns. If either of the two doesn’t exist, the system will create it.

Creating the Domain Model Class

This section will define the model class for the JAVA and PostgreSQL Web Application .

First, create a package for the model class by right-clicking on the src folder. Select “File” and then select “Package.” Create a meaningful package name for the new model class. For this example, “com.chinito.carsmanager.model” will be used. The following screenshot provides an example:

image shows the New Java Package window

Next, create a model class named Cars.java by right clicking on the package com.chinito.carsmanager.model and then selecting “New” and then “Class” to open the New JAVA Class window. The results should resemble the following screenshot:

image show the New Java Class window

Now create a meaningful name for the class, something that is both easy to remember and read. Now click the “Finish” button to create the model class.

This JAVA class will match the fields of the table within the PostgreSQL table. This class will be annotated as an entity class using the @Entity annotation that will link with the cars table in the PostgreSQL database.

The JAVA code follows:

@Entity
public class Cars {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String carname;
private String brand;
private String remarks;

public Cars() {
super();

}


public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}


public String getCarname() {
return carname;
}
public void setCarname(String carname) {
this.carname = carname;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}

}

A breakdown of the above JAVA code follows:

  • An @Entity annotation is used to tell Hibernate this class can be persisted.

  • The @Column annotation maps the column id property in the cars table.

  • The @Id annotation defines the JPA id and will conform to the object table’s primary key.

  • The GenerationType.IDENTITY annotation tells Hibernate the value will be populated by the table itself, and not by the code.

  • The remainder of the code was generated using the setters and getters for setting and getting values.

Conclusion

This tutorial was the fourth part in the series explaining how to create a JAVA and PostgreSQL Web Application from scratch with ECLIPSE, Spring Data, Tomcat and other databases. Part four of this tutorial covered how to configure persistence in Eclipse IDE and provided a step-by-step explanation of that code. This section also explained how to create a model class for the JAVA and PostgreSQL web application, how to create the domain model class and gave a breakdown of that code. Remember that a good working knowledge and understanding of the codes presented in previous sections of the tutorial series are needed before preceding with next sections.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.