Simple Spring Hibernate App


Open Spring Tool Suite

File -> New -> Spring Template Project

Persistence -> Simple Spring Hibernate Utility Project

Project Name: hibernateExample

Top Level Package: com.skills421.examples.hibernate01

Finish

Modifying the Code to Extend our Simple JDBC App

File -> New -> Class

Package: com.skills421.examples

Name: Person

Now Create a simple POJO called Person as follows:

package com.skills421.examples;

import java.io.Serializable;

public class Person implements Serializable
{
private static final long serialVersionUID = 1L;

private Integer personId;
private String name;
private Integer age;
private Float height;

public Person()
{
}

public Person(String name, Integer age, Float height)
{
	this.name = name;
	this.age = age;
	this.height = height;
}

    // getters and setters
}

We want to use ToStringBuilder so let’s add the Apache Commons Lang dependency to the Pom.xml

Whilst we’re at it, let’s also add the Postgres Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.springframework.samples.spring</groupId>
	<artifactId>spring-hibernate-utility</artifactId>
	<version>1.0.0.CI-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>Spring Hibernate Utility</name>
	<url>http://www.springframework.org</url>
	<description>
		<![CDATA[This project is a minimal jar utility with Spring configuration for Hibernate.]]>
	</description>
	<properties>
		<maven.test.failure.ignore>true</maven.test.failure.ignore>
		<spring.framework.version>3.0.6.RELEASE</spring.framework.version>
	</properties>

	<dependencies>

		<!-- *** Commons-Lang *** -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>20030203.000129</version>
		</dependency>

		<!-- *** Postgres *** -->
		<dependency>
			<groupId>postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.2-1002.jdbc4</version>
		</dependency>

		<!-- *** Hibernate *** -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>3.6.0.Final</version>
		</dependency>

		<!-- *** JUnit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>

		<!-- *** Spring *** -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.framework.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>

		<!-- *** Log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>org.jboss.repository.releases</id>
			<name>JBoss Maven Release Repository</name>
			<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>

		<!-- *** Postgres Repository *** -->
		<repository>
			<id>java.net</id>
			<url>http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar</url>
		</repository>

	</repositories>

</project>

Now let’s go back to our POJO and add the Hibernate Annotations and toString method to give us:

package com.skills421.examples;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
public class Person implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer personId;
    private String name;
    private Integer age;
    private Float height;

    public Person()
    {

    }

    public Person(String name, Integer age, Float height)
    {
	this.name = name;
	this.age = age;
	this.height = height;
    }

    // getters and setters

    public String toString()
    {
	return new ToStringBuilder(this)
			.append(personId)
			.append(name)
			.append(age)
			.append(height)
			.toString();
    }
}

Now let’s modify the HibernateConfiguration to connect to Postgres, as follows

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s