我正在使用Spring Data JPA并尝试运行这样的查询:
@Query(value="select i.content from Item i where i.itemOrder = :order")
List<String> findContentByItemOrder(@Param("order")Integer itemOrder);
但它抛出异常: org.hibernate.hql.internal.ast.QuerySyntaxException:项目未映射[从项目i中选择i.content,其中i.itemOrder =:order]
所以当我运行时:
@Query(value="select i.content from package.name.Item i where i.itemOrder = :order")
List<String> findContentByItemOrder(@Param("order")Integer itemOrder);
一切都像预期的那样有效。我的配置:
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("package.name");
factory.setDataSource(dataSource);
factory.setJpaProperties(createProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
private Properties createProperties(){
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
return properties;
}
我正在使用:Spring Data Jpa 1.3.1.RELEASE,Hiberante 4.2,Spring 3.2.1
问题是我不想在我的Query中为每个类名设置 package.name 。如何设置Spring Data JPA以自动查找映射类?