-->
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.  [ 8 posts ] 
Author Message
 Post subject: Lazy loading does not work as advertised
PostPosted: Sat Jan 02, 2010 11:45 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
I have a very large query that returns 1000 objects (called Fielding) .Each Fielding object has a single Master object.
My domain Fielding object has the Master object set as null an I do not reference it ,yet no matter how I configure my Fielding.hbm.xml file ,it runs a separate select query for the Master object . I only need to access a Master object on a case by case basis . I do not need all of them loaded at once and it is causing a performance nightmare.

Can anyone tell me what is wrong with my configuration files ?

Nothing I do is working as the Hibernate documentation says it will. I have also tried to add lazy="no-proxy" because apparently (from other posts) lazy="true" does not work for anybody.

Thanks for taking a look.

Fielding.hbm.xml
Code:


<hibernate-mapping>
   <class name="baseball.hibernate.Fielding" table="fielding">
<cache usage="read-write" />
      <composite-id name="id" class="baseball.hibernate.FieldingId">
         <key-property name="playerId" type="string">
            <column name="playerID" length="9" />
         </key-property>
         <key-property name="yearId" type="short">
            <column name="yearID" />
         </key-property>
         <key-property name="stint" type="short">
            <column name="stint" />
         </key-property>
         <key-property name="pos" type="string">
            <column name="POS" length="2" />
         </key-property>
      </composite-id>

      <property name="playerId" type="string" insert="false" update="false" >
         <column name="playerID" length="9" />
      </property>

      <property name="yearId" type="short" insert="false" update="false">
         <column name="yearID" />
      </property>
      <property name="stint" type="short" insert="false" update="false">
         <column name="stint" />
      </property>
      <property name="pos" type="string" insert="false" update="false">
         <column name="POS" length="2" />
      </property>
      
      <property name="teamId" type="string">
         <column name="teamID" length="3" not-null="true" />
      </property>
      <property name="lgId" type="string">
         <column name="lgID" length="2" not-null="true" />
      </property>
      <property name="g" type="java.lang.Short">
         <column name="G" />
      </property>
      <property name="gs" type="java.lang.Short">
         <column name="GS" />
      </property>
      <property name="innOuts" type="java.lang.Short">
         <column name="InnOuts" />
      </property>
      <property name="po" type="java.lang.Short">
         <column name="PO" />
      </property>
      <property name="a" type="java.lang.Short">
         <column name="A" />
      </property>
      <property name="e" type="java.lang.Short">
         <column name="E" />
      </property>
      <property name="dp" type="java.lang.Short">
         <column name="DP" />
      </property>
      <property name="pb" type="java.lang.Short">
         <column name="PB" />
      </property>
      <property name="lahmanId" type="java.lang.Short">
            <column name="lahmanID" />
            </property>
      
         <property name="wp" type="java.lang.Short">
         <column name="WP" />
         </property>
         <property name="sb" type="java.lang.Short">
         <column name="SB" />
         </property>
         <property name="cs" type="java.lang.Short">
         <column name="CS" />
         </property>
         
      <property name="nameFirst" type="string" insert="false" update="false" >
         <column name="nameFirst" length="50" />
      </property>
            <property name="nameLast" type="string" insert="false" update="false" >
         <column name="namelast" length="80" /></property>
      <property name="zr" type="java.lang.Double">
         <column name="ZR" precision="5" scale="3" />
         
      </property>
      <many-to-one  name="master" class="baseball.hibernate.Master" column="playerId" property-ref="playerId"   />




Master.hbm.XML
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 18, 2009 3:03:12 PM by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="baseball.hibernate.Master" table="master">
   <cache usage="read-write" />
        <id name="lahmanId" type="java.lang.Short">
            <column name="lahmanID" />
            <generator class="identity" />
        </id>
        <property name="playerId" type="string">
            <column name="playerID" length="10" unique="true" not-null="true" />
        </property>
        <property name="managerId" type="string">
            <column name="managerID" length="10" not-null="true" />
        </property>
        <property name="hofId" type="string">
            <column name="hofID" length="10" not-null="true" />
        </property>
        <property name="birthYear" type="java.lang.Short">
            <column name="birthYear" />
        </property>
        <property name="birthMonth" type="java.lang.Short">
            <column name="birthMonth" />
        </property>
        <property name="birthDay" type="java.lang.Short">
            <column name="birthDay" />
        </property>
        <property name="birthCountry" type="string">
            <column name="birthCountry" length="50" />
        </property>
        <property name="birthState" type="string">
            <column name="birthState" length="2" />
        </property>
        <property name="birthCity" type="string">
            <column name="birthCity" length="50" />
        </property>
        <property name="deathYear" type="java.lang.Short">
            <column name="deathYear" />
        </property>
        <property name="deathMonth" type="java.lang.Short">
            <column name="deathMonth" />
        </property>
        <property name="deathDay" type="java.lang.Short">
            <column name="deathDay" />
        </property>
        <property name="deathCountry" type="string">
            <column name="deathCountry" length="50" />
        </property>
        <property name="deathState" type="string">
            <column name="deathState" length="2" />
        </property>
        <property name="deathCity" type="string">
            <column name="deathCity" length="50" />
        </property>
        <property name="nameFirst" type="string">
            <column name="nameFirst" length="50" />
        </property>
        <property name="nameLast" type="string">
            <column name="nameLast" length="50" not-null="true" />
        </property>
        <property name="nameNote" type="string">
            <column name="nameNote" />
        </property>
        <property name="nameGiven" type="string">
            <column name="nameGiven" />
        </property>
        <property name="nameNick" type="string">
            <column name="nameNick" />
        </property>
        <property name="weight" type="java.lang.Short">
            <column name="weight" />
        </property>
        <property name="height" type="java.lang.Double">
            <column name="height" precision="4" scale="1" />
        </property>
        <property name="bats" type="string">
            <column name="bats" length="2" />
        </property>
        <property name="throws_" type="string">
            <column name="throws" length="2" />
        </property>
        <!-- 
        -->
        <property name="debut" type="java.sql.Date">
            <column name="debut" />
        </property>
       
        <property name="finale" type="java.sql.Date">
            <column name="finalGame"  />
        </property>
       
        <property name="college" type="string">
            <column name="college" length="50" />
        </property>
        <property name="lahman40id" type="string">
            <column name="lahman40ID" length="9" />
        </property>
        <property name="lahman45id" type="string">
            <column name="lahman45ID" length="9" />
        </property>
        <property name="retroId" type="string">
            <column name="retroID" length="9" />
        </property>
        <property name="holtzId" type="string">
            <column name="holtzID" length="9" />
        </property>
        <property name="bbrefId" type="string">
            <column name="bbrefID" length="9" />
        </property>

    </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Sun Jan 03, 2010 1:36 am 
Newbie

Joined: Fri Sep 11, 2009 11:16 am
Posts: 3
Hey there,

I know lazy loading works because I use it all the time ;-)

You're problem is that your configuration/design isn't quite right. Your many-to-one configuration specifies a property-ref="playerId" but your Master class's id is "lahmanId".

In your Fielding class the lahmanId property is the actual Foreign Key to the Master class
So what you need to do is get rid of the current many-to-one definition you have in your Fielding class and replace the


Code:
<property name="lahmanId" type="java.lang.Short">
         <column name="lahmanID" />
</property>


with

Code:
<many-to-one name="lahmanId" class="baseball.hibernate.Master"/>


Does that make sense? You will need to modify your Java classes appropriately, so that you won't have a getLahmanId() in your Fielding class. Instead, to access the lahmanId from the Fielding class you will have to do getMaster().getLahmanId()

Cheers
Johnny


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Sun Jan 03, 2010 6:53 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Thanks for getting back to me Johnny.

What your saying does make sense . If I understand what you are saying, the "many" side of any relationship has to have a foreign key to the "one" side regardless of direction.

The problem is I have series of mapping files that need to map to Master.hbm.xml via the player id because they don't have a lahmanId by default. Please see Batting.hbm.xml below. The lahmanId in fielding was a field that was mistakenly generated via the hbm to ddl setting being set to on then somehow got copied back . I've since turned it off as it really muddies what is going with the data structures.

As you pointed out,I'm sure I must be able to use lazy loading but it is some configuration issue.
Your help is appreciated.

Batting.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 18, 2009 3:03:12 PM by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="baseball.hibernate.Batting" table="batting" >
     <cache usage="read-write" />
        <composite-id name="id" class="baseball.hibernate.BattingId">
            <key-property name="playerId" type="string">
                <column name="playerID" length="9" />
            </key-property>
            <key-property name="yearId" type="short">
                <column name="yearID" />
            </key-property>
            <key-property name="stint" type="short">
                <column name="stint" />
            </key-property>
        </composite-id>
                <property name="yearId" type="short" insert="false" update="false">
                <column name="yearID" />
            </property>

             <property name="playerId" type="string" insert="false" update="false">
                <column name="playerID" length="10" />
            </property>     
                 <property name="stint" type="short" insert="false" update="false">
                <column name="stint" />
            </property>       
        <property name="teamId" type="string">
            <column name="teamID" length="3" not-null="true" />
        </property>
        <property name="lgId" type="string">
            <column name="lgID" length="2" not-null="true" />
        </property>
        <property name="g" type="java.lang.Short">
            <column name="G" />
        </property>
        <property name="gbatting" type="java.lang.Short">
            <column name="G_batting" />
        </property>
        <property name="ab" type="java.lang.Short">
            <column name="AB" />
        </property>
        <property name="r" type="java.lang.Short">
            <column name="R" />
        </property>
        <property name="h" type="java.lang.Short">
            <column name="H" />
        </property>
        <property name="2b" type="java.lang.Short">
            <column name="2B" />
        </property>
        <property name="3b" type="java.lang.Short">
            <column name="3B" />
        </property>
        <property name="hr" type="java.lang.Short">
            <column name="HR" />
        </property>
        <property name="rbi" type="java.lang.Short">
            <column name="RBI" />
        </property>
        <property name="sb" type="java.lang.Short">
            <column name="SB" />
        </property>
        <property name="cs" type="java.lang.Short">
            <column name="CS" />
        </property>
        <property name="bb" type="java.lang.Short">
            <column name="BB" />
        </property>
        <property name="so" type="java.lang.Short">
            <column name="SO" />
        </property>
        <property name="ibb" type="java.lang.Short">
            <column name="IBB" />
        </property>
        <property name="hbp" type="java.lang.Short">
            <column name="HBP" />
        </property>
        <property name="sh" type="java.lang.Short">
            <column name="SH" />
        </property>
        <property name="sf" type="java.lang.Short">
            <column name="SF" />
        </property>
        <property name="gidp" type="java.lang.Short">
            <column name="GIDP" />
        </property>
        <property name="nameLast" type="string" insert="false" update="false" >
      <column name="namelast" length="80" /></property>
     
             <many-to-one  name="master" column="playerID" entity-name="baseball.hibernate.Master" property-ref="playerId"  insert="false" update="false" />
       
           
       <!--
        <property name="GOld" type="java.lang.Short">
            <column name="G_old" />
        </property>
        -->
    </class>
   
</hibernate-mapping> 


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Mon Jan 04, 2010 6:45 am 
Newbie

Joined: Fri Sep 11, 2009 11:16 am
Posts: 3
Hey Paul,

You might need to think about your design because in theory any entity that needs have a many-to-one relationship with Master should have Master's primary key (lahmanId) specified as a foreign key.

Now I'm not sure how your Domain model is constructed and what the exact relationships between the entities are (I can tell it has something to do with sets of statistics for Baseball players).

What I'm wondering is what is the Master Entity all about? Do you have more than one Master for each player? If not then maybe you should make the playerId the primary key and not the lahmanId. (I don't even know what a lahmanId is???)


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Mon Jan 04, 2010 9:21 am 
Newbie

Joined: Tue Jun 02, 2009 4:06 am
Posts: 16
Hi ,

<many-one > tag can also refer to a fiield which is non-primary key.Though it is not advisable but still you can use it.

An exeprt take from Hibernate Reference:

The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is a complicated and confusing relational model. For example, if the Product class had a unique serial number that is not the primary key. The unique attribute controls Hibernate's DDL generation with the SchemaExport tool.

<many-to-one name="product" property-ref="serialNumber" column="PRODUCT_SERIAL_NUMBER"/>

So i feel the mapping file is ok.

-Kartik


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Mon Jan 04, 2010 1:38 pm 
Newbie

Joined: Fri Sep 11, 2009 11:16 am
Posts: 3
Yes you're right Kartik. Although the property-ref does not have to be an explicit primary key, it is important however that the property-ref is a unique identifier hence making it an alternate key.

We haven't yet established in this case whether or not the playerId is an alternate key in the Master entity.


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Mon Jan 04, 2010 2:29 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Your right in that the design is not ideal . It was generated from an existing/legacy database using hibernate tools .
So, it follows the model Kartik suggested.

The layout is basically I have tables for pitching ,batting ,fielding. Each has a playerId as part of the primary key. The other parts of the key are a field for a year and field for a stint(number of times a player played for the same team in a given year) . Each of these table maps to exactly one row (playerId) in the Master table .The Master table has details like birth city ,date of birth ,etc. PlayerId is unique in master but not the other tables and is the only common field . The lahmanId in Fielding was a hibernate generated mistake .


Top
 Profile  
 
 Post subject: Re: Lazy loading does not work as advertised
PostPosted: Mon Jan 04, 2010 3:20 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2008 6:41 pm
Posts: 20
Hi,
Something that may or may not be helpful is the section of code that causing it to get all the master records :

Code:
      SessionFactory factory = HibernateUtil.getSessionFactory();
   
      Session session = factory.getCurrentSession();
      session.getTransaction();
      Transaction tx = session.beginTransaction();
       retVals.addAll(session.createQuery("from baseball.hibernate.Batting where  yearID = " + yr).list());

      int count = 0;
      baseball.hibernate.Batting b;
      tx.commit();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.