-->
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.  [ 3 posts ] 
Author Message
 Post subject: Get id without loading object
PostPosted: Fri Nov 04, 2005 4:32 am 
Beginner
Beginner

Joined: Wed Sep 07, 2005 9:57 am
Posts: 20
Hibernate version: 3.0.5

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 package="com.carma.console.authorization">

    <class name="Authorization" table="AUTH_AUTHORIZATION" lazy="true">
    <!-- Meta attributes for code generation -->   
          <meta attribute="scope-field">protected</meta>
          <meta attribute="implement-equals">true</meta><meta attribute="extends">com.carma.console.BaseHibernateObject</meta> 
      <meta attribute="generated-class">com.carma.console.authorization.model.AbstractAuthorization</meta>     
           
    <!-- Id -->     
        <id name="id" column="AUTHORIZATION_ID" type="long">
         <meta attribute="scope-set">protected</meta>       
               <meta attribute="use-in-tostring">true</meta>            
              <generator class="sequence">
                      <param name="sequence">AUTHORIZATION_SEQ</param>
              </generator>
        </id>
       
         
    <!-- A versioned entity -->
      <version name="version" column="VERSION"  access="org.hibernate.property.DirectPropertyAccessor">
          <meta attribute="scope-field">protected</meta>
          <meta attribute="scope-set">protected</meta>
          <meta attribute="scope-get">protected</meta>         
      </version>

        <!-- many-to-one name="authAuthorizationTemplate" column="AUTHORIZATION_TEMPLATE_ID" class="AuthorizationTemplate" /-->

        <many-to-one name="companyGroup" column="COMPANY_GROUP_ID" class="com.carma.console.authorization.company.CompanyGroup"  not-null="true">
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>

        <many-to-one name="documentTypeGroup" column="DOCUMENT_TYPE_GROUP_ID" class="com.carma.console.authorization.doctype.DocumentTypeGroup"  not-null="true">
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>       

        <many-to-one name="functionGroup" column="FUNCTION_GROUP_ID" class="com.carma.console.authorization.function.FunctionGroup">
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>               

        <many-to-one name="role" column="ROLE_ID" class="com.carma.console.authorization.user.Role">
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>       

        <many-to-one name="category" column="CATEGORY_ID" class="com.carma.console.common.Category"  not-null="true">
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>       

        <many-to-one name="user" column="USER_ID" class="com.carma.console.authorization.user.User">          
           <meta attribute="use-in-equals">true</meta>    
        </many-to-one>       

        <property name="statusFlag" column="STATUS_FLAG" type="string"  not-null="true" length="1" />
    </class>
   
</hibernate-mapping>


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 package="com.carma.console.common">

    <class name="Category" table="CATEGORY" lazy="true" mutable="false">
    <!-- Meta attributes for code generation -->   
          <meta attribute="scope-field">protected</meta>
          <meta attribute="implement-equals">true</meta><meta attribute="extends">com.carma.console.BaseHibernateObject</meta> 
      <meta attribute="generated-class">com.carma.console.common.model.AbstractCategory</meta>     
           
    <!-- Id -->     
        <id name="id" column="CATEGORY_ID" type="long">
         <meta attribute="scope-set">protected</meta>       
               <meta attribute="use-in-tostring">true</meta>                    
              <generator class="sequence">
                      <param name="sequence">CATEGORY_SEQ</param>
              </generator>
        </id>               

        <set name="documentTypeGroups" cascade="save-update" lazy="true" inverse="true">
            <key column="CATEGORY_ID"/>
            <one-to-many class="com.carma.console.authorization.doctype.DocumentTypeGroup"/>
        </set>

        <set name="roles"  cascade="save-update" lazy="true" inverse="true">
            <key column="CATEGORY_ID"/>
            <one-to-many class="com.carma.console.authorization.user.Role"/>
        </set>

        <set name="documentTypes" cascade="save-update" lazy="true" inverse="true">
            <key column="CATEGORY_ID"/>
            <one-to-many class="com.carma.console.common.DocumentType"/>
        </set>

        <set name="functions" cascade="save-update" lazy="true" inverse="true">
            <key column="CATEGORY_ID"/>
            <one-to-many class="com.carma.console.authorization.function.Function2Category"/>
        </set>
   
      <many-to-one name="profileDefinition" column="PROFILE_ID" class="com.carma.console.common.ProfileDefinition" />   
   
        <property name="name" column="CATEGORY_NAME" type="string"  not-null="true">
            <meta attribute="use-in-tostring">true</meta>      
           <meta attribute="use-in-equals">true</meta>    
        </property>           
    </class>
   
</hibernate-mapping>





The generated SQL (show_sql=true):
Code:
DEBUG 2005-11-04 09:10:06,009 - select authorizat0_.USER_ID as USER8_1_, authorizat0_.AUTHORIZATION_ID as AUTHORIZ1_1_, authorizat0_.AUTHORIZATION_ID as AUTHORIZ1_0_, authorizat0_.VERSION as VERSION0_0_, authorizat0_.COMPANY_GROUP_ID as COMPANY3_0_0_, authorizat0_.DOCUMENT_TYPE_GROUP_ID as DOCUMENT4_0_0_, authorizat0_.FUNCTION_GROUP_ID as FUNCTION5_0_0_, authorizat0_.ROLE_ID as ROLE6_0_0_, authorizat0_.CATEGORY_ID as CATEGORY7_0_0_, authorizat0_.USER_ID as USER8_0_0_, authorizat0_.STATUS_FLAG as STATUS9_0_0_ from AUTH_AUTHORIZATION authorizat0_ where authorizat0_.USER_ID=?
DEBUG 2005-11-04 09:10:06,109 - select companygro0_.COMPANY_GROUP_ID as COMPANY1_0_, companygro0_.VERSION as VERSION1_0_, companygro0_.NAME as NAME1_0_, companygro0_.STATUS_FLAG as STATUS4_1_0_ from AUTH_COMPANY_GROUP companygro0_ where companygro0_.COMPANY_GROUP_ID=?
DEBUG 2005-11-04 09:10:06,209 - select documentty0_.DOCUMENT_TYPE_GROUP_ID as DOCUMENT1_0_, documentty0_.VERSION as VERSION4_0_, documentty0_.CATEGORY_ID as CATEGORY3_4_0_, documentty0_.NAME as NAME4_0_, documentty0_.STATUS_FLAG as STATUS5_4_0_ from AUTH_DOCUMENT_TYPE_GROUP documentty0_ where documentty0_.DOCUMENT_TYPE_GROUP_ID=?
DEBUG 2005-11-04 09:10:06,300 - select role0_.ROLE_ID as ROLE1_0_, role0_.VERSION as VERSION7_0_, role0_.CATEGORY_ID as CATEGORY3_7_0_, role0_.NAME as NAME7_0_, role0_.STATUS_FLAG as STATUS5_7_0_ from AUTH_ROLE role0_ where role0_.ROLE_ID=?
DEBUG 2005-11-04 09:10:06,400 - select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.PROFILE_ID as PROFILE2_13_0_, category0_.CATEGORY_NAME as CATEGORY3_13_0_ from CATEGORY category0_ where category0_.CATEGORY_ID=?



Hi,

I have an Authorization object refering to a few other objects, eg. Category. Now I load the Authorization object and then want to access the IDs of the refered objects, eg. the category-id. The category class is set to lazy. I thought that the category would only be loaded when i accessed any of the non-id properties because those are obviously not known then, but for some reason even though I only access the Id (which should be known by the authorization-object) hibernate is querying the whole category object in the database.


authorization = (Authorization) authorizations.next();
authorization.getCompanyGroup().getId().toString();
authorization.getCategory().getId().toString();
authorization.getDocumentTypeGroup().getId().toString();

Any comment is appreciated.
Thanks, Ronald


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 4:35 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
check out Session.getIdentifier(Object object) . how far does that get you?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 5:18 am 
Beginner
Beginner

Joined: Wed Sep 07, 2005 9:57 am
Posts: 20
Hi Dennis,

thanks for your answer. It did not directly solve my problem but got me thinking. The real problem was that I used the category, etc. in the equals/hashcode methods of the authorization object and then in the equals/hashcode methods of the category i used the category name. When I got the iterator of the authorization-set those got invoked and had to load the category object.

But in general both the direct .getId() and Session.getIdentifier(Object object) work without having to load the whole object.

Thanks again for getting me thinking in the right direction.

Ronald


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