我遇到了 Spring JPA 关于排序和分页的问题。目前我收到此警告:
WARN [NamedQuery] (NamedQuery.java:65) - Finder method public abstract org.springframework.data.domain.Page loc.starterkit.business.entities.application.repository.ApplicationRepository.findByNameLike(java.lang.String,org.springframework.data.domain.Pageable)
is backed by a NamedQuery but contains a Pageable parameter! Sorting delivered via this Pageable will not be applied!
我的 JPA 接口代码如下所示:
public interface ApplicationRepository extends PagingAndSortingRepository<Application,Long>{
Page<Application> findByNameLike(String name, Pageable pageable);
}
我的实体工厂配置:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="packagesToScan" value="loc.starterkit.business"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceUnitName" value="general" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.connection.charSet">${hibernate.connection.charSet}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
</props>
</property>
</bean>
我不知道为什么在这种情况下我不能在可分页方法中使用排序。有任何想法吗?
编辑:
我的应用实体
@Entity
@Table(name="APPLICATION")
public class Application {
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="DESCRIPTION")
private String description;
public Application() {
super();
}
.... (getters and setters here)
}
将 PagingAndSortingRepository 更改为 JPARepository 似乎会引发相同的警告。但是,经过一些测试,我发现这个 WARN 没有任何意义。事情似乎运作良好:
Hibernate:
/* select
count(*)
from
Application as generatedAlias0
where
generatedAlias0.name like :param0 */ select
count(*) as col_0_0_
from
APPLICATION applicatio0_
where
applicatio0_.NAME like ?
Hibernate:
/* select
generatedAlias0
from
Application as generatedAlias0
where
generatedAlias0.name like :param0
order by
generatedAlias0.name asc */ select
applicatio0_.ID as ID0_,
applicatio0_.DESCRIPTION as DESCRIPT2_0_,
applicatio0_.NAME as NAME0_
from
APPLICATION applicatio0_
where
applicatio0_.NAME like ?
order by
applicatio0_.NAME asc limit ?
所以我认为这个 WARN 没有任何意义,但我想知道为什么要发出它