-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: Including foreignKey along with foreignObject - many-to-one
PostPosted: Mon Mar 15, 2004 3:25 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Question related to hibernate mapping of POJOs.

1. Is it possible
1.1 to have in POJO object foreign field without foreign object
1.2 or have both, but achieve in some cases to don't load foreign objects?

In some cases, I don't want to lookup for foreign objects, but just read only from one table - base table, and also write directly to this table without using foreign keys.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 7:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please reformulate

_________________
Emmanuel


Top
 Profile  
 
 Post subject: example
PostPosted: Mon Mar 15, 2004 8:25 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Example of code

Code:
package com.domainmodel;

/**
* @hibernate.class table="INSTRUMENT_HISTORY_TBL"
* @jboss-net.xml-schema urn="MyApp:InstrumentHistory"
*/
public class InstrumentHistory implements java.io.Serializable {
    public Long id;
    public Long instrumentId;
    public Instrument instrument;
    // etc....

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

    /**
     * @hibernate.id
     */
    public Long getId() {
        return id;
    }

    public void setInstrumentId(Long instrumentId) {
        this.instrumentId = instrumentId;
    }

    /**
     * This is foreign key - I wold like map also this field
     */
    public Long getInstrumentId() {
        return instrumentId;
    }

    /**
     * This is getter for foreign entity
     *
     * @hibernate.many-to-one column="instrumentId" class="com.domainmodel.Instrument"
     */
    public Instrument getInstrument() {
        return instrument;
    }
    // etc....
}


What I would like:
1.2.1 I would like optionality - read list of InstrumentHistory objects without loading instrument objects inside instrumentHistory.

1.2.2 I would like optionally save instrumentHistory object, while instrumentId is set, not instrument. Or more exactly, while one of the (instrumentId or instrument) fields are set.

f.e.
Code:
InstrumentHistory ih = new InstrumentHistory();
ih.setInstrumentId(new Long(10));
session.save(ih);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 8:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it seems dangerous for me.
You can map the field OR the object (association) but both.... harder to manage in the code.
You must be sure that InstrumentHistory.instrumentId and InstrumentHistory.instrument.instrumentId are always the same.
One of the advantages of ORM is making fk fields implicits using associations...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 8:44 am 
Newbie

Joined: Wed Mar 03, 2004 10:19 am
Posts: 11
Location: Netherlands
It is possible to include in your mapping both the foreign key properties and the associated reference property. As these properties both map on the same database columns, one of them needs to have insert='false' update='false' in the mapping.

sample:

<class ...>

....

<property name="instrumentId" insert="false" update="false">
<column name="INSTR_ID"/>
</property>

<many-to-one name="instrument" ...>
<column name="INSTR_ID"/>
</many-to-one>

....

<class>


Top
 Profile  
 
 Post subject: performance
PostPosted: Mon Mar 15, 2004 8:48 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
In my case, Instrument object have very much fields, that contains next foreign objects....

So when I would like to read simple record from db, I must read all graph.

Or when I need to write high number of records (instrument objects), I must lookup for InstrumentHistory object for every instrument, and then set this instrument to instrumentHistory, before I do session.save(instrument).

Because on input I have instrumentId, not Instrument object as whole.
Data comes from another language, layer...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 8:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
lazy loading is done for that... not enough for you?

is this case, you should consider "broke" the association and only use field


Top
 Profile  
 
 Post subject: lazy loading
PostPosted: Mon Mar 15, 2004 8:59 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
It is possible to use lazy loading in my case?
Can you provide som example - based on mine?

Because I thought that lazy loading is for one-to-many association, not for many-to-one. I am wrong?


Top
 Profile  
 
 Post subject: reply to Fridoo
PostPosted: Mon Mar 15, 2004 9:05 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
reply to Fridoo

It seems to be good idea, I will check it.
But I need getInstrument() -> insert="false" update="false"

Also I need to force hibernate to generate database schema with foreign keys.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 9:28 am 
Newbie

Joined: Wed Mar 03, 2004 10:19 am
Posts: 11
Location: Netherlands
You can move the insert/update=false from the property to the many-to-one if you like.

You can use lazy loading by setting the proxy property for your Instrument class (the parent in the many-to-one relationship). See the reference manual for more details


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 2:06 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://forum.hibernate.org/viewtopic.php?t=928928


Top
 Profile  
 
 Post subject: for delpouve
PostPosted: Tue Mar 16, 2004 3:28 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Thanks delpouve but in this link is not answer for my question
Quote:
http://forum.hibernate.org/viewtopic.php?t=928928


Top
 Profile  
 
 Post subject: Re: reply to Fridoo
PostPosted: Tue Mar 16, 2004 4:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
pstruhos wrote:
reply to Fridoo
Also I need to force hibernate to generate database schema with foreign keys.


Schema export doesn't work?


Top
 Profile  
 
 Post subject: solution
PostPosted: Tue Mar 16, 2004 4:28 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Thanks to all and especially Frido - I will change my opinion on Lord of Rings film :-))

Solution of my question looks like this:

TERMINOLOGY
base entity = InstrumentHistory
foreign entity = Instrument

Code:
package com.domainmodel;

/**
* @hibernate.class table="INSTRUMENT_HISTORY_TBL"  proxy="com.domainmodel.InstrumentHistory"
* @jboss-net.xml-schema urn="MyApp:InstrumentHistory"
*/
public class InstrumentHistory implements java.io.Serializable {
    public Long id;
    public Long instrumentId;
    public Instrument instrument;
    // etc....

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

    /**
     * @hibernate.id
     */
    public Long getId() {
        return id;
    }

    public void setInstrumentId(Long instrumentId) {
        this.instrumentId = instrumentId;
    }

    /**
     * This is foreign key - I wold like map also this field
     *
     * @hibernate.property
     */
    public Long getInstrumentId() {
        return instrumentId;
    }

    public void setInstrument(Instrument instrument) {
        /**
         * If foreign entity is set, also foreign key
         * in base entity is synchronized because
         * only value from instrument.instrumentId is
         * updated to database
         */
        if (instrument != null) {
            instrumentId = instrument.getInstrumentId();
        }
        this.instrument = instrument;
    }

    /**
     * This is getter for foreign entity
     *
     * @hibernate.many-to-one column="instrumentId" class="com.domainmodel.Instrument" insert="false" update="false"
     */
    public Instrument getInstrument() {
        return instrument;
    }
    // etc....
}


With this class mapping:

    * it is possible directly set instrumentId to Instrument object

    * while foreign entity is set, also Instrument.instrumentId is set via code in setInstrument() - kind of synchronization

    * also foreign keys in schema generation are generated right (one of these (foreign key or foreign entity) must be mapped as insert="false" update="false" )

    * can be read object (optionally) without foreign entity and also with foreign entity (lazy loading - proxy="com.domainmodel.InstrumentHistory")

Examples of code.

Reading without loading of foreign entity - Example 1:
Code:
obj = session.load(InstrumentHistory.class, new Long(10));
Hibernate.initialize(obj);
session.close();


Reading without loading of foreign entity - Example 2:
Code:
try {
  results = (List) session.find("from InstrumentHistory");
} catch (HibernateException he) {
     he.printStackTrace();
} finally {
     session.close();
}



Reading with loading of foreign entity - Example 3:
Code:
obj = session.load(InstrumentHistory.class, new Long(10));
Hibernate.initialize(obj);
Hibernate.initialize(obj.getInstrument());
session.close();


Top
 Profile  
 
 Post subject: for delpouve
PostPosted: Tue Mar 16, 2004 4:44 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Earlier I was trying to do something like Frido recommended, but I did not include insert="false" update="false" to one of these fields:
    getInstrument()
    getInstrumentId()


Maybe this was reason of bad schema generation and some other problems, I don't remember exactly.

Now everything works ok - see my previous post.

Thanks a lot boys, now I am happy with hibernate, because it was crucial problem for me while using Hibernate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.