我有这张桌子:
@Entity
@Table(name = "system_accounts")
@Getter
@Setter
public class SystemAccounts {
@Id
@Column(name = "id", nullable = false)
private UUID id;
@Column(name = "user_code")
@Enumerated(EnumType.STRING)
private AccountType userCode;
}
.......
public enum AccountType {
PERSONAL,
CORPORATE
}
我使用这个 SQL 查询:
@Query(value = """
SELECT ba.id,
FROM system_accounts
AND ba.user_code IN :accountTypes
""", nativeQuery = true)
Optional<List<SystemAccounts>> findAll(@Param("accountTypes") Set<AccountType> accountType);
我收到错误:
ERROR: operator does not exist: character varying = smallint
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
你知道我如何使用
Set<AccountType>
来发送 IN 子句的参数列表吗?
您的问题源于您的组合
@Enumerated(EnumType.STRING)
和本机查询。
您可以按照以下方式组合它们:
我可以在 JpaRepository nativeQuery 中使用枚举参数吗?
或者您可以以 JPQL 格式重写查询。
例如而不是修复一些语法错误后调整的:
@Query(value = """
SELECT ba.id,
FROM system_accounts ba
WHERE ba.user_code IN :accountTypes
"""
, nativeQuery = true)
Optional<List<SystemAccount>> findAll(@Param("accountTypes") Set<AccountType> accountType);
你会写:
@Query(value = """
SELECT a
FROM SystemAccounts a
WHERE a.userCode IN :accountTypes
""")
List<SystemAccount> findAllAccountType(@Param("accountTypes") Set<AccountType> accountType);