问题描述
我正在尝试使用命名查询获取具有一对一关系的实体。我的“where”条件在关系实体上。我给出了一个命名参数。当我执行查询时,它忽略传递的参数并给我所有记录。 我试过位置参数,它也没有用。
@NamedQuery(
name = "country.by.region",query = " select c from Country c join Region r on r.id = :regid"
)
国家实体
public class Country {
@Id
@Column(name = "COUNTRY_ID")
private String id;
@Column(name = "COUNTRY_NAME")
private String name;
@OnetoOne(targetEntity = Region.class,cascade = CascadeType.ALL)
@JoinColumn(name = "REGION_ID")
private Region region;
// ...
}
区域实体
public class Region {
@Id
@Column(name = "REGION_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "regSeq")
private int id;
@Column(name = "REGION_NAME")
private String name;
// ...
}
DAO 实现
@Override
public List<Country> findBy(Region region) {
TypedQuery<Country> query = getEntityManager().createNamedQuery("country.by.region",Country.class);
query.setParameter("regid",region.getId());
query.setMaxResults(30);
return query.getResultList();
}