-->
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.  [ 5 posts ] 
Author Message
 Post subject: Default eager fetching when using Annotations
PostPosted: Wed Feb 18, 2009 7:24 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
Hello!

I have a simple Domain model. "Dienst" has a property "Benutzer".

When I map the entities with annotations, the "benutzer" property is always fetched eagerly.
When I map the entities with the provided hbm.xml file I get my expected LazyLoadingException

I think I miss something trivial, as I've not found anything in the forums, docus, or books.

as it states here ( http://www.hibernate.org/250.html#A18 ) that by default fetching strategy is set to lazy.

Any help is appreciated,
thx in advance & kind regards


b]Hibernate version:[/b]
hibernate-core: 3.3.1.GA or hibernate 3.2.6 when loading the dependency transient

hibernate-annotations: 3.3.1.GA

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

<hibernate-mapping>

    <class name="com.company.app.types.impl.Dienst" table="tblDienst">
        <id name="id" column="DienstId" />
        <property name="beginn" column="DienstBeginn"/>
        <many-to-one name="benutzer" column="BenutzerId"></many-to-one>
    </class>

</hibernate-mapping>


Code:
package com.company.app.types.impl;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Table(name="tblDienst")
@Entity
public class Dienst implements com.company.app.types.Dienst {

   private String id;
   
   private Date beginn;
   
   private Date ende;
   
   private String benutzerId;

   private Benutzer benutzer;
   
   @Id
   @Column(name="DienstId")
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   
   @ManyToOne
   @JoinColumn(name="BenutzerId")
   public Benutzer getBenutzer() {
      return benutzer;
   }
   public void setBenutzer(Benutzer benutzer) {
      this.benutzer = benutzer;
   }

   @Column(name="DienstBeginn")
   public Date getBeginn() {
      return beginn;
   }
   public void setBeginn(Date beginn) {
      this.beginn = beginn;
   }

   @Column(name="BenutzerId", insertable=false, updatable=false)
   public String getBenutzerId() {
      return benutzerId;
   }
   public void setBenutzerId(String benutzerId) {
      this.benutzerId = benutzerId;
   }

   @Column(name="DienstEnde")
   public Date getEnde() {
      return ende;
   }
   public void setEnde(Date ende) {
      this.ende = ende;
   }
}



Code between sessionFactory.openSession() and session.close():

Code:
Session session = dataAccess.getSessionFactory().openSession();
      
      Query query = session.createQuery("from Dienst d where d.id = ?");
      query.setParameter(0, dienstIds[0]);
            
      Dienst loadedD = (Dienst) query.uniqueResult();
      session.close();
      try{
         loadedD.getBenutzer().getLogin();
         Assert.fail();
                  
      } catch(LazyInitializationException lie)
      {
      } catch(Exception e)
      {
         Assert.fail();
      }



Name and version of the database you are using:
org.hsqldb.jdbcDriver and MS SQL Servier 2000

The generated SQL (show_sql=true):
select dienst0_.DienstId as DienstId2_, dienst0_.DienstBeginn as DienstBe2_2_, dienst0_.BenutzerId as BenutzerId2_, dienst0_.DienstEnde as DienstEnde2_ from tblDienst dienst0_ where dienst0_.DienstId=?

select benutzer0_.BenutzerId as BenutzerId1_1_, benutzer0_.BenutzerGebDatum as Benutzer2_1_1_, benutzer0_.BenutzerKFZKennzeichen as Benutzer3_1_1_, benutzer0_.BenutzerLogin as Benutzer4_1_1_, benutzer0_.MandantenId as Mandante7_1_1_, benutzer0_.BenutzerMitarbeiterNr as Benutzer5_1_1_, benutzer0_.BenutzerPasswort as Benutzer6_1_1_, mandant1_.MandantenId as Mandante1_0_0_, mandant1_.Login as Login0_0_, mandant1_.Name as Name0_0_, mandant1_.Passwort as Passwort0_0_ from tblBenutzer benutzer0_ left outer join tblMandant mandant1_ on benutzer0_.MandantenId=mandant1_.MandantenId where benutzer0_.BenutzerId=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:15 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Hi,
You are right, xml and annotations behave different. In annotation guide it says:
Quote:
@OneToOne and @ManyToOne are defaulted to EAGER

Hibernate referencesays:
Quote:
By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations


I am not sure, why the behavior is different, but I guess Annotations behave that way, because they are (JPA-compliant) JPA-annotations and your xml-mapping is a hibernate mapping.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:21 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
many thx!

I can't believe I didn't saw that in the docu.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:28 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You are welcome. I also haven't thought of that difference until I saw your post. Then it came back to my mind...

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Default eager fetching when using Annotations
PostPosted: Wed Jul 13, 2011 7:27 am 
Newbie

Joined: Wed Jul 13, 2011 7:25 am
Posts: 2
Do you know if it is possible to override the eager fetching globably by means of configuration?


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