Spring Data JPA:使用原始值搜索值对象?
Spring Data JPA: search value object using raw value?
我有一个用值对象定义为字段的实体:
class CustomerEntity {
@Id
@Column(name = "customer_id", nullable = false)
private CustomerId customerId;
}
CustomerId 定义为值对象:
class CustomerId implements Serializable {
private final String value;
//constructor, getter omitted for brewity
}
存储库也很容易定义并且运行良好
interface CustomerRepository extends JpaRepository<CustomerEntity, CustomerId> {
Optional<Customer> findByCustomerId(CustomerId customerId);
}
如何定义 Spring 数据存储库,以便我可以使用字符串值在 cusomter_id 上搜索客户?
Optional<Customer> findByCustomerIdStartsWith(String search);
类似SELECT * FROM Customers where customer_id like '?%'
的东西?据我所知,我需要使用带有 @Query
注释的自定义 JpaExpression。但我不知道如何将值对象字段转换为字符串...
UPD 1
已找到解决方法,但看起来有点难看...
@Query("FROM CustomerEntity e where concat(e.customerId, '') like ?1%")
Optional<Customer> findByCustomerIdStartsWith(String search);
UPD 2
所以我找到了让它工作的唯一方法 - 将值对象设为 @Embeddable
并在其他对象中将其用作 @Embedded
和 @AttributeOverride
。然后 aswers 中建议的解决方案将起作用。作为一个缺点,我需要为值对象创建空构造函数。
Documentation 提及 findByFirstnameLike
。 findByCustomerIdLike 对您有用吗?
另请查看 advanced like 表达式。
像link这样的进阶提到:
@Query("select u from User u where u.firstname like %?1")
对于 findByFirstnameEndsWith
。
所以我认为它应该适用于
@Query("select c from Customer c where c.customerId.value like ?1%")
List<Customer> findByCustomerIdStartsWith(String id);
我之前没有扩展答案,因为另一个答案在我尝试添加详细信息时提到了非常相似的内容。我想它应该与 CustomerId
中实现的 equals 方法一起使用
在 @Query
中使用 JPQL,并使用访问器导航至 CustomerId#value
。使用 @Param("search")
将命名参数传递到查询中并将其称为 :search
- 它也适用于 like 'something%'
构造。
@Query(SELECT * FROM Customers c where c.customerId.value like ':search%'")
Optional<Customer> findByCustomerIdStartsWith(@Param("search") String search);
我有一个用值对象定义为字段的实体:
class CustomerEntity {
@Id
@Column(name = "customer_id", nullable = false)
private CustomerId customerId;
}
CustomerId 定义为值对象:
class CustomerId implements Serializable {
private final String value;
//constructor, getter omitted for brewity
}
存储库也很容易定义并且运行良好
interface CustomerRepository extends JpaRepository<CustomerEntity, CustomerId> {
Optional<Customer> findByCustomerId(CustomerId customerId);
}
如何定义 Spring 数据存储库,以便我可以使用字符串值在 cusomter_id 上搜索客户?
Optional<Customer> findByCustomerIdStartsWith(String search);
类似SELECT * FROM Customers where customer_id like '?%'
的东西?据我所知,我需要使用带有 @Query
注释的自定义 JpaExpression。但我不知道如何将值对象字段转换为字符串...
UPD 1 已找到解决方法,但看起来有点难看...
@Query("FROM CustomerEntity e where concat(e.customerId, '') like ?1%")
Optional<Customer> findByCustomerIdStartsWith(String search);
UPD 2
所以我找到了让它工作的唯一方法 - 将值对象设为 @Embeddable
并在其他对象中将其用作 @Embedded
和 @AttributeOverride
。然后 aswers 中建议的解决方案将起作用。作为一个缺点,我需要为值对象创建空构造函数。
Documentation 提及 findByFirstnameLike
。 findByCustomerIdLike 对您有用吗?
另请查看 advanced like 表达式。
像link这样的进阶提到:
@Query("select u from User u where u.firstname like %?1")
对于 findByFirstnameEndsWith
。
所以我认为它应该适用于
@Query("select c from Customer c where c.customerId.value like ?1%")
List<Customer> findByCustomerIdStartsWith(String id);
我之前没有扩展答案,因为另一个答案在我尝试添加详细信息时提到了非常相似的内容。我想它应该与 CustomerId
中实现的 equals 方法一起使用在 @Query
中使用 JPQL,并使用访问器导航至 CustomerId#value
。使用 @Param("search")
将命名参数传递到查询中并将其称为 :search
- 它也适用于 like 'something%'
构造。
@Query(SELECT * FROM Customers c where c.customerId.value like ':search%'")
Optional<Customer> findByCustomerIdStartsWith(@Param("search") String search);