I am trying to return a list of Hibernate entities from a list of ids. The following piece of code is responsible:
String sql = "select id from foo where some condition is met";
List<Long> ids = jdbcTemplate.queryForList(sql, Long.class); // 3 ids ..
List<FooEntity> list = ((Session) this.entityManager.getDelegate())
.createCriteria(FooEntity.class)
.add(Restrictions.in("id", ids)).list(); // .. -> 60 entities
...
@Entity
public class FooEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@OneToMany(
targetEntity = BarEntity.class,
cascade = { CascadeType.ALL },
fetch = FetchType.EAGER)
@JoinTable(
name = "foo_bar",
joinColumns = @JoinColumn(name = "foo_id"),
inverseJoinColumns = @JoinColumn(name = "bar_id"))
private List<BarEntity> bars;
...
}
When the code has executed I have a list of 3 ids and a list of 60 FooEntity instances!
Under normal circumstances I expect these lines of code to deliver just as many entities as ids. Any ideas what could cause this strange behaviour?
EDIT: Reacting to Thomas' comment below I found out that there are exactly 20 Bar instances for each Foo entity. And every FooEntity is also returned 20-fold.