Hibernate API中的ElementCollection createAlias [英] ElementCollection createAlias in hibernate API

查看:144
本文介绍了Hibernate API中的ElementCollection createAlias的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有人知道是否以及如何解决以下问题(写在JPA API中)可以使用hibernate标准API编写吗? 更具体地说,我有一个讨论实体包含参与者列表(这是一个用户名列表):

  @ElementCollection 
@Column(name = user_name)
@CollectionTable(name =DISCUSSION_USER,joinColumns = @JoinColumn(name =DISCUSSION_ID))
@OrderColumn(name =ORDER_INDEX)
private List< String> ;参与者=新的ArrayList< String>();

现在我需要检索给定用户名是参与者的所有讨论。



如果我为Participant创建了一个实体,这将很简单:

  Criteria crit = 。的getSession()个createCriteria(Discussion.class); 
crit.createAlias(参与者,p);
crit.add(Restrictions.eq(p.userName,portalUsername));

但我无法用非实体创建别名...

解决方案

正如这里我想要的是不可能的:


使用ElementCollection代替OneToMany的限制是目标对象不能被查询,坚持,独立于其父对象合并。它们是严格私有的(依赖)对象,与嵌入式映射相同。在ElementCollection上没有级联选项,目标对象总是被持久化,合并,并与父级一起被移除。


所以,我用一个OneToMany来代替。


does anyone know if and how the solution for following question (which is written in the JPA API) can be written using the hibernate criteria API?

To be more specific I have a Discussion entity that contains a list of participants (which is a list of usernames):

@ElementCollection
@Column(name = "user_name")
@CollectionTable(name = "DISCUSSION_USER", joinColumns = @JoinColumn(name = "DISCUSSION_ID"))
@OrderColumn(name = "ORDER_INDEX")
private List<String> participants = new ArrayList<String>();

Now I need to retrieve all Discussions where a given username is a participant.

If I would have created an entity for Participant this would be straightforward:

    Criteria crit = getSession().createCriteria(Discussion.class);
    crit.createAlias("participants", "p");
    crit.add(Restrictions.eq("p.userName", portalUsername));

But I can't create an alias with a non entity...

解决方案

As explained here what I wanted is not possible:

The limitations of using an ElementCollection instead of a OneToMany is that the target objects cannot be queried, persisted, merged independently of their parent object. They are strictly privately-owned (dependent) objects, the same as an Embedded mapping. There is no cascade option on an ElementCollection, the target objects are always persisted, merged, removed with their parent.

So, I used a OneToMany instead.

这篇关于Hibernate API中的ElementCollection createAlias的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆