Hibernate,按复合键的属性进行查询

时间:2013-05-27 03:17:27

标签: hibernate criteria

以下是我的实体,没有明确的Hibernate注释。

class Main
{
    private SomePk pk;
}

class SomePk
{
    private A a;
    private B b;
}

class A
{
    private String aProperty;
}
class B
{
    private String bProperty;
}

我想做一些查询:

session.createCtitera(Main.class);
        .add(Restrictrions.eq("pk.a.aProperty", "foo"))
        .list();

但这会抛出异常“无法解析属性:pk.a.aPropery of:Main”。那么如何编写条件查询?

=============更新=======================

=== DAO方法===

public Set<Main> loadMains()
{
    Session session = sessionFactory.getCurrentSession();
    List list = session.createCriteria(Main.class)
        .createAlias("pk", "p_alias")
        .createAlias("p_alias.a", "a_alias")
        .add(Restrictions.eq("a_alias.aProperty", "j"))
        .list();

    Set<Main> mains = new LinkedHashSet<Main>(list);
    return mains;
}

=== Class Main ===

@Entity
@Table(name = "MAIN")
public class Main
{
    private SomePK pk;

    private String name;

    @EmbeddedId
    public SomePK getPk()
    {
        return pk;
    }

    public void setPk(SomePK pk)
    {
        this.pk = pk;
    }

    @Column(name = "NAME")
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

=== Class SomePK ===

// ingnore import and package header 

@Embeddable
public class SomePK implements Serializable
{
    private static final long serialVersionUID = 1L;

    private A a;

    private B b;

    @ManyToOne
    @JoinColumn(name = "A")
    public A getA()
    {
        return a;
    }

    public void setA(A a)
    {
        this.a = a;
    }

    @ManyToOne
    @JoinColumn(name = "B")
    public B getB()
    {
        return b;
    }

    public void setB(B b)
    {
        this.b = b;
    }

}

=== A类===

@Entity
@Table(name = "A")
public class A
{
    private String aProperty;

    private long id;

    @Id
    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    @Column(name = "A_PROPERTY")
    public String getaProperty()
    {
        return aProperty;
    }

    public void setaProperty(String aProperty)
    {
        this.aProperty = aProperty;
    }

}

=== B类===

@Entity
@Table(name = "B")
public class B
{
    private String bProperty;

    private long id;

    @Id
    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    @Column(name = "B_PROPERTY")
    public String getbProperty()
    {
        return bProperty;
    }

    public void setbProperty(String bProperty)
    {
        this.bProperty = bProperty;
    }

}

谢谢!

1 个答案:

答案 0 :(得分:3)

Criteria API仍然不直接支持嵌套属性。您需要创建Alias来创建嵌套属性的Criteria。以下代码应该适合您!

 session.createCriteria(Main.class)
 .createAlias("pk.a", "a_alias")
 .add( Restrictions.eqProperty("a_alias.aProperty", "foo")) // .add( Restrictions.eq("a_alias.aProperty", "foo"))
 .list();

不鼓励使用Criteria来处理复杂情况。尽可能尝试对这些情况使用动态生成的静态查询。这将使您可以控制查询,以便在可能的情况下维护和优化它们。

干杯和快乐的编码!