MDP
MDP

Reputation: 4277

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

Upvotes: 0

Views: 881

Answers (2)

Vitor Santos
Vitor Santos

Reputation: 611

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>

Upvotes: 2

Lokesh Kumar Gaurav
Lokesh Kumar Gaurav

Reputation: 726

Try this code I hope it will be useful for you.

//Repository Code

public interface ArtistaRepository extends JpaRepository<Artista,Integer> {
    Page<Artista> findByNome(String nome,Pageable page);  
}

//Service code

int totalElements;
    int totalPages;
    int numberOfElements;

Pageable pageable = new PageRequest(page,size,Sort.Direction.DESC,"id");

Page<Artista> list=artistaRepository.findByNome(nome,pageable);

totalElements=list.getTotalElements();
numberOfElements=list.getNumberOfElements();
totalPages=list.getTotalPages();

Upvotes: 0

Related Questions