简体   繁体   English

Hibernate映射:HQL多对一查询 - 如何检索交叉pojo对象的属性

[英]Hibernate mapping : HQL many-to-one Query - how to retrieve attributes of crossed pojo objects

I'm a beginner using Hibernate 4.3(xml style) and I'm stuck with the way to query when it asks not only the pojo elements. 我是一个使用Hibernate 4.3(xml风格)的初学者,当我不仅询问pojo元素时,我仍然坚持查询的方式。 What I mean...(see next example) 我的意思......(见下一个例子)

I have two tables : Person and Adress. 我有两张桌子:人和地址。 In table Person, I have a foreign key 'id_adress_fk' who references the Adress. 在表Person中,我有一个引用Adress的外键'id_adress_fk'。 In table Adress, I have an 'id' as primary Key and 'Adress_Street_Name' as attribute. 在表Adress中,我将'id'作为主键,将'Adress_Street_Name'作为属性。 One participant can have only one Adress. 一位参与者只能拥有一个地址。 One adress have several person who lives there. 一个地址有几个人住在那里。 I write two Pojos class. 我写了两个Pojos课。

I'm trying to request the database to have Person attributes and Adress Street Name. 我正在尝试请求数据库具有Person属性和Adress Street Name。 But I don't get to retrieve the Adress Street Name. 但我无法检索Adress Street Name。 I don't understand what object is returned, how to write the good query who matches the good object returned. 我不明白返回什么对象,如何编写匹配返回的好对象的好查询。 Do i have to make a join between Adress and Person ? 我必须在地址和人之间建立联系吗? But in this case what is the object returned ? 但在这种情况下返回的对象是什么? How can I handle this ? 我怎么处理这个?

Thanks a lot. 非常感谢。

My pojos : 我的pojos:

public class Person implements Serializable  {

private Integer id;
private Adress adress;

    // getters and setters

public class Adress implements Serializable {

private Integer id;
private Set<Person> persons   = new HashSet<Person>(0);

    // getters and setters

My mappings : 我的映射:

ADRESS.HBM.XML ADRESS.HBM.XML

    <set name="persons" table="person" inverse="true" lazy="false" >

        <key>
            <column name="id_adress_fk" not-null="true"  />
        </key> 
        <one-to-many class="com.you.know.what.Person" />
</set>

PERSON.HBM.XML PERSON.HBM.XML

    <many-to-one name="adress" class="you.know.what.Adress" >
        <column name="id_adress_fk" not-null="true" /> 
    </many-to-one>  

My query (for instance): 我的查询(例如):

 List<Person> allPerson = getSessionFactory().getCurrentSession().createQuery( "from Person ").list();

I know i'm querying only the Pojo Person, but the Adress object is 'included' in Person, so i should sucess to retrieve Adress_street_name...A hand on this could be great ! 我知道我只查询Pojo Person,但是Adress对象被“包含”在Person中,所以我应该成功检索Adress_street_name ......这样做可能很棒!

According to the default fetching strategy of Hibernate, when you load an entity, the associated entities (or the collections mapped to this entity) are not loaded by default. 根据Hibernate的默认提取策略,当您加载实体时,默认情况下不会加载关联的实体(或映射到此实体的集合)。 So, when you are loading the list of Person : 因此,当您加载Person列表时:

( "from Person ").list()

the associated Address entity will not be loaded, a proxy of Address will be loaded instead. 将不会加载关联的Address实体,而是加载Addressproxy A proxy is a placeholder instance of a runtime-generated subclass of a mapped persistent class ( Address ) that delegates all method invocations to a different instance of Address that is fetched lazily from the database. proxy是映射的持久化类( Address )的运行时生成的子类的占位符实例,它将所有方法调用委托给从数据库中懒惰地获取的另一个Address实例。

To initialize the proxies you need to have the Session open and the objects to be attached to it. 要初始化代理,您需要打开Session并将对象附加到它。 That means Hibernate can not fetch data anymore when you already ended your unit of work and closed the Session, or when you detached objects from it. 这意味着当您已经结束工作单元并关闭Session或者从中分离对象时,Hibernate不再能够获取数据。

But you can override this default fetching strategy at runtime in code; 但您可以在运行时在代码中覆盖此默认提取策略; so that, whenever needed, you can load Person along with their Address : 这样,只要需要,您就可以加载Person及其Address

getSessionFactory().getCurrentSession().createQuery( "from Person p left join fetch p.adress").list();

With this query you will have a list of Person along with their Address . 使用此查询,您将获得Person列表及其Address

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

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