查询与Hibernate标准的ManyToMany关系 [英] Querying ManyToMany relationship with Hibernate Criteria

查看:418
本文介绍了查询与Hibernate标准的ManyToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何描述这个问题,所以我认为一个例子是问我问题的最好方法:

我有两张表,其中包含manyToMany关系:

DriversLicence< - > LicenceClass



LicenceClass是类似于Car,Motorbike,和Medium Rigid。

使用Hibernate Criteria,我怎么能找到所有拥有Car和MotorbikeLicenceClass的许可证?



UPDATE 12/11/2008
我发现使用自定义ResultTransformer可以轻松实现。然而,问题是只有在查询返回结果后才会应用结果转换器,结果转换器实际上不会成为SQL的一部分。所以我想现在我的问题是:你能做我最初在SQL中描述过的东西 - 是否有Hibernate Criteria模拟? 解决方案

以下是我最终如何使用HQL实现它:

  public List< DriversLicence> findDriversLicencesWith(List< LicenceClass> licenceClasses){
String hqlString =从DriversLicenceImpl dl中选择dl,其中1 = 1;
for(int i = 0; i< licenceClasses.size(); i ++){
hqlString + =and:licenceClass+ i += some elements(dl.licenceClasses);
}

Query query = getSession()。createQuery(hqlString);
for(int i = 0; i< licenceClasses.size(); i ++){
query.setParameter(licenceClass+ i,licenceClasses.get(i));
}
返回query.list();
}

或者使用Hibernate Criteria和sqlRestriction:


(LicenceClass licenceClass:licenceClasses){
criteria.add(Restrictions.sqlRestriction(?= some(选择+ LicenceClass.PRIMARY_KEY +)的

  + 
LICENCE_CLASS_JOIN_TABLE +where {alias}。+
DriversLicence.PRIMARY_KEY +=+ DriversLicence.PRIMARY_KEY +),
licenceClass.getId(),Hibernate.LONG) );

$ / code>

LICENCE_CLASS_JOIN_TABLE是hibernate为支持many-to driverLicence和LicenceClass之间的许多关系。


I'm not sure how to describe this problem, so I think an example is the best way to ask my question:

I have two tables with a manyToMany relationship:

DriversLicence <-> LicenceClass

LicenceClass is things like "Car", "Motorbike", and "Medium Rigid".

Using Hibernate Criteria, how can I find all licences that have both "Car" and "Motorbike" LicenceClasses?

UPDATE 12/11/2008 I have discovered that this can easily be achieved by using a custom ResultTransformer. However the problem is that a result transformer only gets applied AFTER the query returns its results, it does not actually become part of the SQL. So I guess my question is now "Can you do what I initially described in SQL - and is there a Hibernate Criteria analog?"

解决方案

Here's how I finally achieved it using HQL:

public List<DriversLicence> findDriversLicencesWith(List<LicenceClass> licenceClasses) {
    String hqlString = "select dl from DriversLicenceImpl dl where 1=1 ";
    for (int i = 0; i < licenceClasses.size(); i++) {
        hqlString += " and :licenceClass" + i + " = some elements(dl.licenceClasses)";
    }

    Query query = getSession().createQuery(hqlString);
    for (int i = 0; i < licenceClasses.size(); i++) {
        query.setParameter("licenceClass" + i, licenceClasses.get(i));
    }
    return query.list();
}

Or using Hibernate Criteria with an sqlRestriction:

for (LicenceClass licenceClass : licenceClasses) {               
    criteria.add(Restrictions.sqlRestriction("? = some(select " + LicenceClass.PRIMARY_KEY + " from " +
                    LICENCE_CLASS_JOIN_TABLE + "  where {alias}." +
                    DriversLicence.PRIMARY_KEY + " = " + DriversLicence.PRIMARY_KEY + ")",
                    licenceClass.getId(), Hibernate.LONG));
}

LICENCE_CLASS_JOIN_TABLE is the name of the table that hibernate generates to support the many-to-many relationship between driversLicence and LicenceClass.

这篇关于查询与Hibernate标准的ManyToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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