简体   繁体   中英

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. 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. In table Adress, I have an 'id' as primary Key and 'Adress_Street_Name' as attribute. One participant can have only one Adress. One adress have several person who lives there. I write two Pojos class.

I'm trying to request the database to have Person attributes and Adress Street Name. But I don't get to retrieve the 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 :

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

    <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

    <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 !

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. So, when you are loading the list of Person :

( "from Person ").list()

the associated Address entity will not be loaded, a proxy of Address will be loaded instead. 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.

To initialize the proxies you need to have the Session open and the objects to be attached to it. 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.

But you can override this default fetching strategy at runtime in code; so that, whenever needed, you can load Person along with their 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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