【发布时间】:2023-04-06 15:23:01
【问题描述】:
基本上我得到了以下实体(由 Lombok 扩展)
@Getter
@Setter
@Entity("FOO")
public class Foo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
private long id;
@ManyToOne
@JoinColumn(name = "FK_FEE", nullable = false)
private Fee fee;
@Column(name = "CCCS")
@Convert(converter = StringListConverter.class)
private List<String> cccs;
}
还有 StringListConverter:
@Converter
public class StringListConverter implements AttributeConverter<List<String>, String> {
@Override
public String convertToDatabaseColumn(final List<String> list) {
String returnValue = null;
if (list != null) {
final List<String> trimmedList = new ArrayList<>();
for (final String strg : list) {
if (strg != null && !strg.isEmpty()) {
trimmedList.add(strg.trim());
}
}
returnValue = String.join(",", trimmedList);
}
return returnValue;
}
@Override
public List<String> convertToEntityAttribute(final String joined) {
List<String> returnValue = null;
if (joined != null) {
returnValue = new ArrayList<>();
final String[] splitted = joined.split(",");
for (final String strg : splitted) {
if (strg != null && !strg.isEmpty()) {
returnValue.add(strg.trim());
}
}
}
return returnValue;
}
}
现在我想获取Foo 的列表,其中Fee.Id= 123 和Foo.cccs 包含特定的字符串值。
@Repository
public interface FooRepository extends CrudRepository<Foo, Long> {
List<Foo> findByFeeIdAndCccsIn(Long feeId, String ccc);
}
但这不起作用。是通过编写自己的查询来解决这个问题的唯一方法吗?
【问题讨论】:
-
findByFeeIdAndCccsContaining(Long feeId, String ccc);我觉得是这样的 -
无法为方法 public abstract java.util.List FooRepository.findByFeeIdAndCccsContaining(java.lang.Long,java.lang.String) 创建查询!未知集合表达式类型...
-
是的...从未使用转换后的列对其进行测试。看来你得写一个查询
标签: java spring-data-jpa data-access-layer