AndroidDev
AndroidDev

Reputation: 16375

Using Spring with Hibernate and JPA

I am new to learning Spring. I will be probably using Spring for both Rest API and WebApplications. I am getting quite confused at the difference between using JPA and Hibernate. Hibernate seems to be hassle to configure with Spring ? Would it be better to focus on learning JPA, Hibernate or Both.

Kind Regards

Upvotes: 0

Views: 508

Answers (3)

Veena Nair
Veena Nair

Reputation: 101

I would prefer hibernate because once you are familiar with this then you wont find any difficulty with JPA. Hibernate is comparatively harder but worth learning.

Upvotes: 0

Ernestas Kardzys
Ernestas Kardzys

Reputation: 1727

As @NimChimpsky mentioned, Hibernate is one of implementations of JPA (Java Persistence API).

Well, I can tell from my experience that configuring Hibernate + Spring is not so difficult. For example, my applicationContext.xml contains:

<!-- My datasource -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        <property name="jndiName">
            <value>java:/MY_APPLICATION</value>
        </property>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</prop>
                <prop key="hibernate.show_sql">true</prop>
                 </props>
        </property>

        <property name="mappingResources">
            <list>
                <value>path/to/my/package/Test.hbm.xml</value>
            </list>
        </property>
    </bean>

Then I simply autowire SessionFactory in my DAOs and that's it.

So, personally I would vote for Hibernate :)

Interesting tutorial about Hibernate 4 and Spring 3.x: http://www.javacodegeeks.com/2013/05/hibernate-4-with-spring.html

P.S. My pom.xml contains the following dependencies:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.6.Final</version>
</dependency>

P.P.S. Well, my post is quite messy. But my bottom line is: learn Hibernate and Spring + Hibernate is easy to setup.

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47280

jpa is the standard, hibernate an implementation of. Go with JPA. And use spring-data, so you don't have to do as much programming.

Upvotes: 0

Related Questions