本文介绍了休眠条件:通过另一个具有EmbeddedId的实体为实体创建别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到更好的问题,所以希望它能显示我的要求.

I couldn't find better questions so I hope it shows what I am asking.

我有3个实体(我只会写对更好阅读真正重要的代码

I have 3 entities ( i will only write code that really matters for better reading

Foo.java

@Data
@Entity
@Table(name = "foo", schema = "pl")
@SequenceGenerator(name = "sequence_generator", sequenceName = "pl.foo_seq", allocationSize = 1)
@EqualsAndHashCode(callSuper = false, exclude = {"Doos" })
@ToString(callSuper = true, exclude = { "Doos" })
    public class Foo extends BaseEntity{
    [...]
    @OneToMany(mappedBy = "pk.foo")
        private Set<FooDooRel> Doos= new HashSet<FooDooRel>();

    public void addDoo(FooDooRel fooDooRel) {
        if (fooDooRel== null) {
            return;
        }

        if (!Doos.contains(fooDooRel)) {
            Doos.add(fooDooRel);
        }

    }
    [...]
    }

FooDooRel.java (具有私有构造函数FooDooRel())

FooDooRel.java(has private contructor FooDooRel())

@Getter
@Setter
@Entity
@Table(name = "foo_doo_rel", schema = "pl")
@EqualsAndHashCode
@ToString(callSuper = true, exclude = { "pk" })
@NoArgsConstructor(access = AccessLevel.PRIVATE)
    public class FooDooRel implements IEntity {
    @EmbeddedId
    private FooDooRelId pk;

    public FooDooRel(Doo doo, Foo foo) {
        if (doo == null || foo == null) {
            throw new IllegalArgumentException("Foo or Doo equals null");
        }
        doo.addFoo(this);
        foo.addDoo(this);
        pk = new FooDooRelId(doo, foo);
    }
    [...]
    }

FooDooRelId.java (具有私有构造函数FooDooRelId())

FooDooRelId.java (has private contructor FooDooRelId())

@Getter
@Setter
@Embeddable
@EqualsAndHashCode
@ToString(callSuper = true, exclude = { "doo", "foo" })
@NoArgsConstructor(access = AccessLevel.PRIVATE)
    public class FooDooRelId implements Serializable {

        @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH })
        @JoinColumn(name = "doo_id")
        private Doo doo;

        @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH })
        @JoinColumn(name = "project_id")
        private Foo foo;

    public FooDooRelId(Doo doo, Foo foo) {
        setDoo(doo);
        setFoo(foo);
    }
    }

Doo.java

@Data
@Entity
@Table(name = "doo", schema = "pl")
@SequenceGenerator(name = "sequence_generator", sequenceName = "pl.doo_seq", allocationSize = 1)
@EqualsAndHashCode(callSuper = false, exclude = {"Foos" })
@ToString(callSuper = true, exclude = { "Foos" })
public class Doo extends BaseEntity{
[...]
@OneToMany(mappedBy = "pk.doo")
    private Set<FooDooRel> Foos= new HashSet<FooDooRel>();
@Column(name = "dispid", nullable = false)
    private String dispid;

public void addFoos(FooDooRel fooDooRel) {
    if (fooDooRel== null) {
        return;
    }

    if (!Foos.contains(fooDooRel)) {
        Foos.add(fooDooRel);
    }

}
[...]
}

现在我必须为过滤器创建别名

Now I have to create Aliases for my filter so

Criteria criteria = getSession().createCriteria(Foo.java, "foo")
criteria.createAlias("Doos", "Disp5", JoinType.LEFT_OUTER_JOIN, Restrictions.eq(Disp5.pk.foo.dispid", "5"));

现在出现第一个错误,当我这样做时我得到这个错误

Now first error, when I do this i get this error

我也尝试过

criteria.createAlias("Foos", "fooRel", JoinType.LEFT_OUTER_JOIN).createAlias("fooRel.pk.doo", "doo5",
        JoinType.LEFT_OUTER_JOIN, Restrictions.eq("dispid", "5"));

criteria.createAlias("Foos", "fooRel", JoinType.LEFT_OUTER_JOIN).createAlias("fooRel.pk.doo", "doo5",
        JoinType.LEFT_OUTER_JOIN, Restrictions.eq("doo5.dispid", "5"));

我了解我必须做第二次JOIN才能与Doo一起加入FooDooRel,但我不知道该怎么做.我可以为FooDooRel创建别名,但是在相同条件下如何更进一步地操作Doo.

I understand i have to do second JOIN to join FooDooRel with Doo but I dont know how to do this. I can create alias for FooDooRel but how, in the same criteria go further to Doo.

第二个,执行此操作时,我必须从同一集合中设置2个别名

second one, i have to do 2 aliases from same Set when I do this

Criteria criteria = getSession().createCriteria(Foo.java, "foo")
criteria.createAlias("Doos", "Disp5", JoinType.LEFT_OUTER_JOIN, Restrictions.eq(Disp5.pk.foo.dispid", "5"));
criteria.createAlias("Doos", "Disp6", JoinType.LEFT_OUTER_JOIN, Restrictions.eq(Disp5.pk.foo.dispid", "6"));

我收到一条错误消息,该路径已重复

I get an error, that path is duplicated

首先,如何通过在Doo中检查dispid将FooDooRel加入Foo,然后如何从同一路径加入2个别名?

So first, how can I join FooDooRel to Foo by checking dispid in Doo, and second how can I join 2 aliases from same path?

我不能使用子查询(DetachedCriteria).

I can't use sub-query (DetachedCriteria).

我不想写很多东西,但是在我之前的问题上有人告诉我我的上帝不好,所以我添加了我认为应该添加的所有内容.这基本上是从通过@ManyToOne连接的3个实体的示例.还有一个实体具有embeddableId.希望您能理解我的实体,我的问题是关于准则的,并通过从第三个实体中添加准则来为第一个实体创建别名.

I didn't want to write alot, but on my previous question someone told my that my god isnt good, so I have added all that i think i should I have added. This is basicly example of 3 entities that are connect by @ManyToOne from "inside". Also one entity had embeddableId. I hope you will understand my entities, my question is about criterias, and creating alias for first entity by adding criterion from third one.

推荐答案

如果有人仍然对此问题感到疑惑-幸运的是,这是休眠状态的bug,尚未修复.

If someone still wonders about this issue - unfortunetly this is a bug in hibernate which havent been fixed yet.

这篇关于休眠条件:通过另一个具有EmbeddedId的实体为实体创建别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:26