Hibernate标准:使用embeddedId通过另一个实体创建实体别名

时间:2013-11-25 12:48:14

标签: java hibernate hibernate-criteria

我找不到更好的问题所以我希望它能说明我的要求。

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

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())

@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())

@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);
    }

}
[...]
}

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

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

现在第一个错误,当我这样做时,我收到此错误

  

无法解析属性:pk.foo.dispid:pl.FooDooRel

我也试过

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。

第二个,当我这样做时,我必须从同一个Set中做2个别名

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"));

我收到错误,该路径重复

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

我无法使用子查询(DetachedCriteria)。

我不想写很多,但根据我之前的问题,有人告诉我,我的上帝不好,所以我添加了所有我认为我应该添加的内容。这是通过@ManyToOne从“内部”连接的3个实体的基本示例。另外一个实体有embeddableId。我希望你能理解我的实体,我的问题是关于标准,并通过添加第三个标准为第一个实体创建别名。

1 个答案:

答案 0 :(得分:2)

如果有人仍对这个问题感到疑惑 - 不幸的是,这是一个尚未解决的hibernate错误。