我想选择所有具有状态为新的条形的Foo实体。
这是我试过的:
@Entity
@Table(name = "FOO")
class Foo {
@Id
@Column(name = "ID")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@OneToMany
private Set<Bar> bars;
//...
}
public interface FooRepository extends CrudRepository<Foo, Long> {
@Query("select m from Foo f where f.bars.status = 'NEW' ")
public Page<Foo> findByBarStatus(Pageable pageable);
}
但我明白了:
org.hibernate.exception.SQLGrammarException: could not prepare statement
我也尝试编写join语句:
select f from Foo f inner join f.bars b where b.status = 'NEW'
答案 0 :(得分:1)
SELECT f FROM Foo AS f JOIN f.bars AS b WHERE b.status = 'NEW'
答案 1 :(得分:1)
我认为你错过了这样的东西
@OneToMany(mappedBy = "foo")
private Set<Bar> bars;
答案 2 :(得分:1)
加入一对多关系有时会很棘手。 (还有另一个答案,你可以参考)
在这种情况下,您可以反向(假设您的关系是双向的),从Bar
侧向上查看:
select bar.foo from Bar bar where bar.status = 'NEW'