我正在使用 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.How 设置 Spring Data JPA 以自动查找映射类的每个类名都放置package.name ?