问题描述
我有这样的实体: @Entity
公共类专辑{
私人整数ID;
private Integer ownerId;
私人字符串名称;
私有字符串描述;
私人创建日期;
@OneToMany @JoinColumn(name =albumId)
私人设置< AlbumUser> users = new HashSet< AlbumUser>();
@OneToMany @JoinColumn(name =albumId)
private Set< Picture> pictures = new HashSet< Picture>();
}
以及另一个:
@Entity
public class Picture {
private Integer id;
private Integer creatorId;
私人整数albumId;
私人创建日期;
私有字符串标题;
私有字符串描述;
@ManyToOne @JoinColumn(name =eventId)
私人事件事件;
}
使用Criteria API我想通过过滤的Picturs获得唯一的AlbumDs。我试过这样的事情:
public相册读取(Integer albumId,Set< Integer> picFilter){
Criteria crit = getCurrentSession()。createCriteria(Album.class,album);
crit.add(Restrictions.idEq(albumId));
if(picFilter!= null&&!picFilter.isEmpty()){
crit = crit.createAlias(album.pictures,picture);
crit.add(Restrictions.in(picture.event.id,picFilter));
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
}
相册resultDs =(相册)crit.uniqueResult();
返回resultDs;
}
在这里,我获得了与相关联的所有图片的相册。他们根本没有被过滤。
当我尝试执行由记录器打印的查询时,我只得到四行,这是具有给定eventId的图片数量,但在相册中我获取了所有图片。
我也尝试了其他的ResultTransformers,但最终得到了很多结果(4)。我错过或做错了?
您无法通过在集合中包含限制来过滤与实体关联的集合的内容查询。该查询只会获取相册。集合的内容可以在访问集合时稍后获取。您只需过滤相册即可仅检索包含带有事件ID的图片的相册。
如果收藏集只包含符合条件的图片,而您会得到一个部分集合,它会导致更新问题,因为Hibernate认为已过滤的项目已被删除,并会更新数据库以反映该更改,实际上是从集合中删除项目。
如果您只想接收集合中的某些项目,则可以使用方法。唯一的问题是,它只支持当前的HQL查询。
I have such entity:
@Entity
public class Album {
private Integer id;
private Integer ownerId;
private String name;
private String description;
private Date created;
@OneToMany @JoinColumn(name = "albumId")
private Set<AlbumUser> users = new HashSet<AlbumUser>();
@OneToMany @JoinColumn(name = "albumId")
private Set<Picture> pictures = new HashSet<Picture>();
}
and another one:
@Entity
public class Picture {
private Integer id;
private Integer creatorId;
private Integer albumId;
private Date created;
private String title;
private String description;
@ManyToOne @JoinColumn(name = "eventId")
private Event event;
}
Using Criteria API I want to get unique AlbumDs with filtered set of Picturs. I try something like this:
public Album read(Integer albumId, Set<Integer> picFilter) {
Criteria crit = getCurrentSession().createCriteria(Album.class, "album");
crit.add(Restrictions.idEq(albumId));
if (picFilter != null && !picFilter.isEmpty()) {
crit = crit.createAlias("album.pictures", "picture");
crit.add(Restrictions.in("picture.event.id", picFilter));
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
}
Album resultDs = (Album) crit.uniqueResult();
return resultDs;
}
And here I get Album with all pictures associated. They're not filtered at all.When I try to execute query printed by a logger, I get only four rows wich is the number of pictures with given eventId, but in the Album I get all pictures.
I also tried other ResultTransformers, but eventualy got many result (4) not distinct one.
What do I miss or do wrong?
You can not filter the content of Collections associated with an entity by including Restrictions on the Collection in the query. The query will only fetch the Albums. The content of the Collection can be fetched later, when the Collection is accessed. All you do is filter the Albums to retrieve only those Albums that contain the Pictures with the event ids.
If the Collection would only contain the Pictures that match your Criteria and you would get a partial Collection it would cause problems on updates, because Hibernate then think the filtered items have been removed and would update the database to reflect that change, actually removing the items from the Collection.
If you want to receive only some items from a Collection you can use the Session.createFilter() method. The only problem is, that it only supports HQL queries currently.
这篇关于Hibernate Criteria API - 过滤集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!