gpt4 book ai didi

java - CrudRepository 过滤器 List 属性中的字符串值

转载 作者:行者123 更新时间:2023-12-02 01:13:41 26 4
gpt4 key购买 nike

基本上我得到了以下实体(由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);
}

但这不起作用。解决这个问题的唯一方法是编写自己的查询吗?

最佳答案

@Repository
public interface FooRepository extends CrudRepository<Foo, Long> {
@Query( "Select foo FROM Foo foo WHERE foo.fee.id = :feeId AND foo.cccs LIKE CONCAT(CONCAT('%', :cccs) ,'%')")
List<Foo> findByFeeIdAndCccsIn(@Param("feeId") Long feeId,@Param("cccs") String ccc);
}

关于java - CrudRepository 过滤器 List<String> 属性中的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58990369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com