简体   繁体   English

初学者休眠:为什么我的休眠查询只返回一个结果,而不是列表?

[英]Beginner hibernate: Why does my hibernate query return only one result, instead of a list?

I'm very new to hibernate, and I'm trying to set up a new method in our PersonDAO. 我刚冬眠,我正在尝试在PersonDAO中建立一种新方法。

My hibernate mapping file looks like this: 我的休眠映射文件如下所示:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.foo.bar.domain">
<class name="Person" table="person">
    <meta attribute="class-description">A Person</meta>
    <id name="id" type="java.lang.Long" column="rid" unsaved-value="null">
        <generator class="native" />
    </id>


    <version name="version" type="integer" column="rversion" unsaved-value="null" />
    <property name="UID"           type="string"  column="UID" length="16" not-null="true" unique="true"/>
    <property name="lastName"         type="string"  column="last_name"  not-null="true" />
    <property name="firstName"        type="string"  column="first_name" not-null="true" />
    <property name="ownDepartment"    type="string"  column="own_department"/>

    <!-- a person has many responsibilities and a responsibility can can assigned to many person -->
    <set name="responsibilities" table="person_responsibility">
        <key column="person_id"/>
        <many-to-many column="responsibility_id" class="Responsibility"/>
    </set>

    <set name="additionalDepartments" table="PERSON_TO_ADDL_DEPARTMENT">    
        <key column="person_id"/>               
        <element column="ADDITIONAL_DEPARTMENT" type="string"/>         
    </set>                                              
</class>

and I've written a method like this, in java, to fetch all the managers from a given department: 并且我在Java中编写了这样的方法,以从给定部门获取所有经理:

public List<Person> getManagerByDepartment(final String givenDepartment){
    List<Person> l = (List<Person>) this.getHibernateTemplate().executeFind(new HibernateCallback<List<Person>>() {

    public List<Person> doInHibernate(Session session) throws HibernateException, SQLException {
        String query = "select p from Person p join p.responsibilities responsibilities join p.additionalDepartments additionalDepartments where responsibilities.name = 'manager' and (p.ownDepartment = :givenDepartment or additionalDepartments = :givenDepartment)";
        List<Person> result = (List<Person>) session.createQuery(query).setString("givenDepartment", givenDepartment).list();
        return result;  
        }
    });

    return l;
}

now I do a manual query in SQL, and I can see that for a given department, there are definitely more than one people who have the additional responsibility 'manager'...why does my method only ever return one person, instead of all of them? 现在我用SQL进行手动查询,我可以看到对于一个给定的部门,肯定有不止一个人担负着“经理”的额外责任...为什么我的方法只能返回一个人,而不是全部他们中的?

I have a strong suspicion that my method, specifically my query, and not the mapping, is the issue, but I can't see what's wrong with it... 我非常怀疑我的方法(特别是我的查询而不是映射)是问题所在,但我看不出问题出在哪里……

I've jumped in at the deep end here, so any help would be very much appreciated. 我已经深入到这里,所以任何帮助将不胜感激。

edit: note, I'm working on hundreds of records, not millions, and this isn't exactly a bottle-neck operation, so I'm not too worried about performance...that said if I'm doing something that's pointlessly wasteful, do point it out 编辑:请注意,我正在处理数百条记录,而不是数百万条记录,这并不是一个瓶颈操作,因此我不太担心性能...也就是说,如果我做的事情毫无意义。浪费,一定要指出

Hard to be sure without the sample data, but when you do join in HQL, it is translated to inner join in SQL. 很难确定没有样本数据,但是当你join的HQL,它被翻译成inner join的SQL。 So, if you know that there should be more than one result with given responsibility then the problem is probably join p.additionalDepartments . 因此,如果您知道给定责任应该有多个结果,那么问题可能出在join p.additionalDepartments

Try this query with left join for additionalDepartments and see if it works 尝试使用带有left join的附加查询查询此查询,看看是否可行

String query = "select p from Person p join p.responsibilities responsibilities left join p.additionalDepartments additionalDepartments where responsibilities.name = 'manager' and (p.ownDepartment = :givenDepartment or additionalDepartments = :givenDepartment)";

您可以通过启用showsql选项来打印休眠查询,并检查正在创建的查询,然后针对数据库进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM