Spring + JPA – Example with Exception/Error – Fixing

with 8 comments

In this post ,I have given a  simple example on how to use JPA in Spring Framework.
You can find the source code of the below example in my Github link here.

I have considered a MusicStore example, where i just get the list of Songs from the database.


Framework: Spring
Persistence API : JPA 2.0
Build : Maven
Database : MySQL
JPA Provider : Hibernate-JPA Provider

While I was working on integrating JPA into Spring , I felt that, the most important part to concentrate on , is the CONFIGURATION area.

So,let’s get started with application-context.xml and persistence.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<context:component-scan base-package="aish.vaishno.*" />    
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
     <property name="entityManagerFactory" ref="entityManagerFactory" />

 <bean id="entityManagerFactory"
p:dataSource-ref="dataSource" p:persistenceUnitName="simple-jpa">
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/MUSIC_STORE"/>
        <property name="username" value="music_store"/>
        <property name="password" value="music_store"/>  

  • DIFFERENCE BETWEEN <context:annotation-config> AND <context:component-scan base-package=”…..”>

<context:annotation-config> -Enables the usage of JPA annotations(@Service, @Component, @Repository, @Controller  and etc.,)
<context:component-scan base-package=”…..”> – This component-scan tag also enables the usage of JPA annotations.

Then the difference ???

(i) annotation-config —> enables the usage of JPA annotations , only in the beans specified in the context.xml whereas (ii) component-scan —> scans through all the packages specified, records all the  project beans having JPA annotations, into this context.xml. Therefore it enables JPA annotaion usage in (beans specified in context.xml)+(Project Beans).

Inshort, <component-scan> extends <annotation-config>.

When we use <component-scan> in the app-context.xml, it’s not necessary to use  <annotation-config> again.

Even if both  the tags are specified, it’s not a problem because, the spring container would take care of running the process only once.

  • <tx:annotation-driven transaction-manager=”transactionManger”/> – It checks for @Transactional annotation  in any of the classes. This tag is like a Transactional Switch that turns on the transactional behaviour.Here Transaction Manager is being injected .
  • <bean id=”transactionManager”>– Here Transaction Manager is setup.

Here EntityManagerFactory is being injected.

  • <bean id=”entityManagerFactory”>– Here EntityManagerFactory is setup .Data Source reference and Persistence UnitName Reference is specified.
  • <bean id=”dataSource”> – DB connection details are specified.
  • Persistence UnitName Reference – Based on the PersistenceUnit name, the corresponding portion of persistence.xml is accessed (a Project can have multiple persistenceUnitNames)


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="" xmlns:xsi="" 
  <persistence-unit name="simple-jpa" transaction-type="RESOURCE_LOCAL">
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.max_fetch_depth" value="3"/> 

Here in persistence.xml,

  • persistence-unitName is declared
  • provider – In this example, I have used Hibernate provider.
  • Entity Bean Class is specified
  • properties- Few settings(like showSQL=true) required are specified.

Configuration part is over. Now that, it’s time to use the annotations in our Dao class to make the Spring – JPA work.


DaoInterface :

public interface IMusicStoreDao {
   public List getMusicList();

DaoImplementation :

@Service(value = "MusicCollections")
@Repository(value = "MusicCollections")
public class MusicStoreDaoImpl implements IMusicStoreDao{

  @PersistenceContext(unitName = "simple-jpa")
    private EntityManager entityManager;

     public List getMusicList(){
     List musicDetailsList= entityManager.createQuery("select c from MusicDetails c").getResultList();
     return musicDetailsList;


public class Executer {
   public static void main(String[] args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("META-INF/app-context.xml");
        IMusicStoreDao musicStoreDao=(IMusicStoreDao) applicationContext.getBean("MusicCollections");
        System.out.println("MusicList: \n"+musicStoreDao.getMusicList());

Now , when you run this main class, for sure , you would only get Errors and Exceptions :-P.
Because, we have not yet added the DEPENDENCY JARS.
Below , I have given a note on the Errors and Exceptions that would arise and the corresponding jars to be added to clear that particular problem.

You can find the Maven dependency tags for the below jars in my Github pom.xml


  • ERRORException in thread “main” java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException. 
  • JAR: javaee5-api.jar . Do not forget to specify the scope as “provided”, otherwise the error will still persist.
  • ERROR: Exception in thread “main” java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/EntityManager.
  • JAR: hibernate-jpa2-api.jar
  • EXCEPTION: ClassNotFoundException : org.hibernate.ejb.HibernatePersistence
  • JAR: hibernate-entitymanager.jar
  • ERROR: NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
  • JARS: slf4j-api.jar, slf4j-jcl-over.jar ( scope-RUNTIME ), slf4j-log4j12.jar ( scope-RUNTIME )
  • Do not forget to add the respective JDBC jar.

Now the program can be executed.



Have a great day 🙂

8 Responses

Subscribe to comments with RSS.

  1. SutoCom

    July 25, 2013 at 3:49 PM

  2. nice. covered all spring components with working sample. Very usefull.


    July 25, 2013 at 4:04 PM

  3. Good. Very straight forward and simple and easy to understand. I suggest write a programming cook book.


    July 25, 2013 at 6:00 PM

  4. interesting this and previous one


    July 25, 2013 at 7:23 PM

  5. good
    i will follow also git

    Kandqazcna Czzaetho

    August 9, 2013 at 10:10 AM

  6. Great . good clarity and conceptual stuff.


    August 17, 2013 at 2:48 AM

  7. I tried 7 diff blog sample.
    but not able to understand and i was not getting output.
    but this one is so simple. so much you know in spring and json in other blog tat u have made me understand the concept so well.
    thanks to u i am also seeing groovy and it makes me understand things on what a powerful language. I think u have got a good english skill and very strong java skill with spring that can make understand everything so well for someone in English is not a primary language.
    Write more.

    Janssen MaesdeVesas

    November 3, 2013 at 5:06 PM

  8. […] Reference: Spring and JPA […]

Leave a Reply to Mandarins Cancel reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: