MDP MDP - 28 days ago 22
Java Question

PagingAndSortingRepository (Spring MCV) and Hibernate

I'm trying to create a pagination system with Spring interface PagingAndSortingRepository.
I'm following some guides on the internet, but i'm stuck at the first step.

I implemented PagingAndSortingRepository this way

public interface ArtistaRepository extends PagingAndSortingRepository<Artista,Integer> {
List<Artista> findByNome(String nome);
}


If I try to run the webapplication I get this error:

No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined


I use Hibernate Session as entity manager. In all example I saw on the Internet, JPA EntityManager was used.

This is the code inside my configuration.xml file:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.jeansedizioni.model"/>
</bean>


Since I'm not using JPA EntityManager, how should I edit my code?

Thank you in advance

Answer

Declaring Hibernate's Session Factory won't work. The Spring-data-jpa repositories will only work with the jpa entity manager, as fellow M. Deinum wrote.

Try this configuration:

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="false"/>
    <property name="database" value="<your database - Oracle, MySQL...>"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="persistenceUnitName" value="default"/>
    <!-- spring based scanning for entity classes>-->
    <property name="packagesToScan" value="com.jeansedizioni.model"/>
</bean>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>