我需要通过实体2的id获取实体1的
有查询参数 - 列表数字 - 实体2的ID 。 以及两个带有 @OneToMany 绑定(bind)的实体
@Entity
class Entity1 {
private Integer id;
private Entity2 entity2;
---------
@OneToMany(cascade = ALL, fetch =FetchType.EAGER)
@Column
public Entity2 getEntity2(){
return entity2;
}
和
@Entity
public class Entity2{
private Integer id;
@ManyToOne(FetchType.LAZY)
private Entity1 entity1;
}
我有工作代码
@Repository
public interface Entity1Repository extends JpaRepository<Entity1, Integer> {
@Query("SELECT c" +
" FROM Entity1 c" +
" inner JOIN c.entity2 a" +
" WHERE a.id IN(?1)")
Set<Entity1> findByEntity2Ids(List<Integer> pEntity2_Ids);
}
我想使用 Spring Data 美化查询
并写下类似的内容
Set<Entity1> findAllByEntity2In(List<Integer> pChannels);
但现在不起作用
我发现了一个异常:
\引起:java.lang.IllegalArgumentException:参数值元素 [2] 与预期类型 [com.test.Entity2 (n/a)] 不匹配
谁知道怎么解决?
谢谢!
最佳答案
UPD
我发现了 Spring Data 的查询:
@Repository
public interface Entity1Repository extends JpaRepository<Entity1, Integer> {
Set<Enity1> findByEntity2In(List<Integer> pEntities2Ids);
}
关于java - 如何使用 IN 语句编写 Spring 数据查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46609390/