Spring Data JPA + JSF + Maven + Mysql using Eclipse IDE
1. Spring Data JPA + JSF +
Maven + MySQL using
Eclipse IDE - Simple
Example to start with
2. I am in the process of learning Spring Data JPA and was trying a
lot to use it with a JSF Web Application created as a Maven
project through Eclipse. I was facing issues and ultimately getting
Null Pointer Exceptions at many places. After searching the
internet for a long time, still I could not find a simple example
which will explain the use of two frameworks together i.e. Spring
Data JPA and JSF.
But, one of my friends (Kunal Laud) helped me to overcome the
issues. I thought of sharing the application here as there might be
many learners like me who are facing similar issues. I hope this
document will be of some help.
1) Create a database table first. Below is the query which I used,
CREATE TABLE `Employee` (
`empid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`salary` int(10) DEFAULT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci COMMENT='utf8_unicode_ci'
2) Then, I created a new maven project through Eclipse. Create a
simple project. Skip archetype selection.
3) Then on the next page, enter desired Group Id and Artifact Id.
Select war as packaging option.
4) Next, the most important thing is to update pom.xml with the
necessary dependencies. Below is the pom.xml file,
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6. <!-- MySQL JDBC connector -->
<!-- If you want to use MySQL, uncomment this dependency declation. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.7.1.RELEASE</version>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- The JSR-303 Bean Validation API library. -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
</dependencies>
<build>
<finalName>EmpManagement</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
7. </build>
</project>
Once you save the pom.xml file, Maven will automatically start
downloading the required dependencies. If not, you can
a) right click on project under Project Explorer
b) Click on Run As
c) Select maven install
5) Next, update the web.xml as follows
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>EmpManagement</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-cl
ass>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
20. 12) Web Application Execution
Hit the below URL,
http://localhost:8080/EmpManagement
Enter the necessary details on the form and click
submit. Below is the console output under Eclipse
IDE.
Creating instance of bean 'employeeBean'
Found injected element on class [com.empmgmnt.ui.EmployeeBean]: AutowiredFieldElement
for com.empmgmnt.services.EmployeeService
com.empmgmnt.ui.EmployeeBean.employeeServiceImpl
Processing injected method of bean 'employeeBean': AutowiredFieldElement for
com.empmgmnt.services.EmployeeService
com.empmgmnt.ui.EmployeeBean.employeeServiceImpl
Returning cached instance of singleton bean 'employeeServiceImpl'
Returning cached instance of singleton bean 'employeeService'
Returning cached instance of singleton bean
'org.springframework.transaction.config.internalTransactionAdvisor'
Autowiring by type from bean name 'employeeBean' to bean named 'employeeServiceImpl'
Returning cached instance of singleton bean
'org.springframework.transaction.config.internalTransactionAdvisor'
Finished creating instance of bean 'employeeBean'
############# Saving ################
############# impl ################
com.empmgmnt.services.EmployeeServiceImpl@6d8d0fdd
############ Saving service ##############
Adding transactional method 'save' with attribute:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'
Creating new transaction with name
[org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Opened session at timestamp: 13938637982
Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@3fe3b1c] for JPA transaction
begin
Obtaining JDBC connection
Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/study]
Obtained JDBC connection
initial autocommit status: true
disabling autocommit
Exposing JPA transaction as JDBC transaction
[org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@5d3f4
031]
Loading entity: [com.empmgmnt.domain.Employee#0]
21. select
employee0_.empid as empid0_0_,
employee0_.address as address0_0_,
employee0_.age as age0_0_,
employee0_.name as name0_0_,
employee0_.salary as salary0_0_
from
Employee employee0_
where
employee0_.empid=?
Hibernate:
select
employee0_.empid as empid0_0_,
employee0_.address as address0_0_,
employee0_.age as age0_0_,
employee0_.name as name0_0_,
employee0_.salary as salary0_0_
from
Employee employee0_
where
employee0_.empid=?
Initializing non-lazy collections
Done entity load
Generated identifier: 0, using strategy: org.hibernate.id.Assigned
Initiating transaction commit
Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@3fe3b1c]
committing
Processing flush-time cascades
Dirty checking collections
Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
Listing entities:
com.empmgmnt.domain.Employee{empid=0, address=Mumbai, India, name=Nikhil, age=30,
salary=30000}
insert
into
Employee
(address, age, name, salary, empid)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
Employee
(address, age, name, salary, empid)
values
(?, ?, ?, ?, ?)
committed JDBC Connection
re-enabling autocommit
Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@3fe3b1c] after transaction
Closing JPA EntityManager
HHH000420: Closing un-released batch
Releasing JDBC connection
Released JDBC connection
HHH000163: Logical connection releasing its physical connection
########### After Saving service ##############
#################### After Saving ##############