Spring Boot with Thymeleaf tutorial, part 3 – Spring Data JPA

In this tutorial I am going to add a possibility of storing added posts in a database. I extend previous project where post form is already implemented. I use H2 in-memory database to simplify project’s complexity. To implement data access layer in the application I use Spring Data JPA.

1. Set up the project as is described in previous post or checkout the source code.

2. Add Maven dependencies to enable Spring Data JPA and H2 database:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</artifactId>
</dependency>

Read more

Spring Boot with Thymeleaf tutorial, part 2 – Forms with validation

In this tutorial I am going to extend previous project and show you how to handle with Forms in Spring Boot and Thymeleaf. I am going to create simple form for post. If post is valid, it will be displayed on the result view.

1. Set up the project as is described in previous post or checkout the source code.

2. pom.xml file is the same like in the previous project. I have only changed name and description.

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jvmhub</groupId>
	<artifactId>spring-boot-thymeleaf-form</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>

	<name>spring-boot-thymeleaf-form</name>
	<description>Spring Boot with Thymeleaf, part 2 - Forms</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	

</project>

Read more

Hibernate and PostgreSQL configuration using persistence.xml and EntityManager

In this tutorial I am going to show you how to use Hibernate with Entity Manager. It is based on my previous tutorial and it is an enhancement of this code. In previous tutorial I was using “hibernate-entitymanager” dependency, so there is no need to do any modifications in pom.xml now.

1. Create persistence.xml file in src/main/resources/META-INF directory. You can also remove hibernate.cfg.xml, but it is not mandatory.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">
	<persistence-unit name="entityManager">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<!-- Annotated entity classes -->
        <class>com.jvmhub.tutorial.entity.AppUser</class>
		<properties>

			<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/jvmhubtutorial" />
			<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
			<property name="hibernate.connection.username" value="user" />
			<property name="hibernate.connection.password" value="password" />
			
			<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
		</properties>
	</persistence-unit>
</persistence>

Read more

Hibernate and PostgreSQL configuration with Maven

In this tutorial I am going to show you how to configure connection between Hibernate and PostgreSQL in Java application using Maven.

 

I am using:

JDK 1.7.0_72
Apache Maven 3.2.1
PostgreSQL 9.3.5
Hibernate 4.2.15.Final

 

1. Generate maven project from archetype

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false -DgroupId=com.jvmhub.tutorial -DartifactId=01-hibernate-conf

 

2. Add fallowing dependencies to you pom.xml file

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jvmhub.tutorial</groupId>
	<artifactId>01-hibernate-conf</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>01-hibernate-conf</name>

	<properties>
		<hibernate.version>4.2.15.Final</hibernate.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- The tutorials use the PostgreSQL 9.3.5 database -->
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.3-1102-jdbc41</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

Read more