hibernate - 从 HibernateCompositeUserType 查询字段时出现 IllegalArgumentException
问题描述
我们使用 Spring Data JPA (v2.0.8.RELEASE) 和 Hibernate (v5.2.x) 作为实现。我们注意到 Spring Data JPA 在尝试查询映射为CompositeUserType
.
想象一下下面的类:
public class Currency {
private String code;
private int decimalPlaces;
// getters and setters
}
以下示例使用此类作为CompositeType
public class A {
@Type(type = "CurrencyUserType")
@Columns(
columns = {
@Column(name = "CODE",
@Column(name = "DECIMAL_PLACES"
}
)
private CurrencyUnit currency;
// getters and setters and constructor
}
Spring 数据存储库如下所示:
public class ARepository extends CrudRepository<A, Long> {
Collection<A> findByCurrencyCode(String code);
}
但是,当我尝试执行findByCurrencyCode
查询时,会引发以下异常:
java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Collection ARepository.findByCurrencyCode(java.lang.String)! Illegal attempt to dereference path source [null.currency] of basic type
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:82)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:208)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:553)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:546)
Hibernate 似乎期望我们用一个对象来查询A
类。Currency
但这意味着我们必须始终指定此对象中的所有字段。
作为替代方案,我们可以@Embeddable
从 Currency 类中创建一个,因为它工作正常。不幸的是,我们不想这样做,因为这会将 Currency 类与 JPA 注释联系起来。我们想避免这种情况,因为我们想在不知道 JPA 的模块中重用该类(我们正在以一种干净的架构风格实现我们的应用程序)。
解决方案
推荐阅读
- linux - 当我在 .vimrc 中有空格时生成文件选项卡
- common-lisp - lisp 基本递归函数。帮我追查
- reactjs - GeoFireStore where 查询
- javascript - Puppeteer Constantly Check for Selector to Click
- google-play-services - Google Play 开发者控制台总下载量统计
- php - Zoho Api Oauth v.2 | 为什么要在用户授权请求中登录?
- java - 如何使用 Spock 框架编写多个单元测试?
- css - CSS 设置 md-autocomplete 标记
- python - Graphene_sqlalchemy 和 flask-sqlalchemy 在什么是有效的 SQLAlchemy 模型上存在分歧?
- java - Java GUI - Buttons not showing up in JPanel