根据Spring Data JPA文档的section 3.4.5,可以使用关键字first
和top
来限制查询方法的结果。
我写了这样的代码:
SysPrefixName findFirstByTableName(String tableName);
但是,运行代码时出现此错误:
结果返回多个元素;嵌套异常是javax.persistence.NonUniqueResultException:result返回多个元素
任何有关解决此问题的帮助都将受到赞赏。
答案 0 :(得分:2)
这个对我有用:
ValueError: In safezip, len(args[0])=3 but len(args[1])=1
答案 1 :(得分:1)
Spring Data JPA 1.7中引入了对这些关键字的支持。有关详细信息,请参阅this ticket。
答案 2 :(得分:0)
如果您是构建数据源中的spring,请添加属性:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
如果您使用自定义数据源:
您必须在数据源bean中将属性添加为属性映射:
@Bean
public LocalContainerEntityManagerFactoryBean sqlServerEntityManagerFactory() {
HashMap<String, String> properties = new HashMap<>();
properties.put("hibernate.dialect",
"org.hibernate.dialect.SQLServer2012Dialect");
LocalContainerEntityManagerFactoryBean factoryBean = new
LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(sqlServerDataSource());
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setJpaPropertyMap(properties);
return factoryBean;
}
答案 3 :(得分:-1)
If you did not specify a number (e.g. findFirst10ByTableName) then a result size of 1 is expected. But since your query returned multiple results then you get the error as your return object may not be a Collection/List object that can be mapped from return result set.