-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can you one-to-one lazy-load a component for same table?
PostPosted: Thu May 10, 2007 8:50 am 
Newbie

Joined: Fri Oct 27, 2006 11:39 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:Oracle 10g

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

I have an Employee Entity that maps all the fields in the table, like:
Name, address, phone, etc., as well as references to several other collections. I also have the Employee mapped to dozens of other Entities as an UPDATEBY/CREATEDBY entity. In these cases, all I really care about is the EMPLOYEE_ID, and the name.

Is there a way to create an Employee entity with just the name, and another valueType POJO, (for example, User), that maps all the other fields from the same table, and IT'S relationships, that can be lazy-loaded as a component, (in Java composition style)? That way, I can keep the Employee class I now have, with all the same properties, and I won't be loading all the other relationships that the User would need until they are actually needed...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 9:18 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Hibernate does not lazy load a POJO's objects untill you call their getter.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 10:20 am 
Newbie

Joined: Fri Oct 27, 2006 11:39 am
Posts: 10
OK, then perhaps I have my mappings set up wrong...

Here is the Employee mapping. The component that I'd like to lazy-load is the EmployeeInfo component. This was gernated from xdoclet with the following annotations:
* @hibernate.component name="employeeInfo"
* @hibernate.component class="com.sas.estars.framework.person.employee.EmployeeInfoImpl"
* @hibernate.component fetch="select"
* @hibernate.component update="true"
* @hibernate.component unique="true"
* @hibernate.component lazy="no-proxy"

I also tried it with explicit mappings, as below, in various combinations:

<component name="employeeInfo"
class="com.sas.estars.framework.person.employee.EmployeeInfoImpl"
fetch="select"
update="true"
unique="true"
lazy="no-proxy"
/>



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.sas.estars.framework.person.employee.EmployeeImpl"
        table="EMPLOYEE"
    >

        <id
            name="id"
            column="employee_id"
            type="java.lang.Long"
        >
            <generator class="native">
                <param name="sequence">uid_employee</param>
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-EmployeeImpl.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="userId"
            type="java.lang.String"
            update="true"
            insert="true"
            column="userId"
        />

        <component
            name="name"
            class="com.sas.estars.framework.person.name.NameImpl"
        >
        <property
            name="firstName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="first_name"
        />

        <property
            name="middleName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="middle_name"
        />

        <property
            name="lastName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="last_name"
        />

        <property
            name="prefix"
            type="java.lang.String"
            update="true"
            insert="true"
            column="prefix"
        />

        <property
            name="suffix"
            type="java.lang.String"
            update="true"
            insert="true"
            column="suffix"
        />

        </component>

        <property
            name="remoteUser"
            type="java.lang.String"
            update="true"
            insert="true"
            column="remote_user"
        />

        <property
            name="salesGroup"
            type="java.lang.String"
            update="true"
            insert="true"
            column="salesGroup"
        />

        <component
            name="employeeInfo"
            class="com.sas.estars.framework.person.employee.EmployeeInfoImpl"
        >

        <many-to-one
            name="employeePreference"
            class="com.sas.estars.framework.person.employee.EmployeePreferenceImpl"
            cascade="all"
            fetch="select"
            update="true"
            insert="true"
            column="employee_preference_id"
        />

        <property
            name="administrator"
            type="java.lang.String"
            update="true"
            insert="true"
            column="administrator"
        />

        <property
            name="changeAccount"
            type="boolean"
            update="true"
            insert="true"
            column="change_account"
        />

        <property
            name="contactExcelImport"
            type="boolean"
            update="true"
            insert="true"
            column="contactExcelImport"
        />

        <many-to-one
            name="countryCodeIso2"
            class="com.sas.estars.framework.customization.lov.ListValueImpl"
            cascade="none"
            fetch="select"
            update="true"
            insert="true"
            column="country_code2_lov_id"
        />

        <property
            name="currentStatus"
            type="java.lang.String"
            update="true"
            insert="true"
            column="currstat"
        />

        <property
            name="departmentCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="department_code"
        />

        <property
            name="departmentName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="deptname"
        />

        <property
            name="developer"
            type="boolean"
            update="true"
            insert="true"
            column="developer"
        />

        <property
            name="divisionCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="divcode"
        />

        <property
            name="division"
            type="java.lang.String"
            update="true"
            insert="true"
            column="division"
        />

        <property
            name="editIndustryTier"
            type="boolean"
            update="true"
            insert="true"
            column="editIndustryTier"
        />

        <property
            name="email"
            type="java.lang.String"
            update="true"
            insert="true"
            column="email"
        />

        <property
            name="gone"
            type="java.lang.String"
            update="true"
            insert="true"
            column="gone"
        />

        <property
            name="location"
            type="java.lang.String"
            update="true"
            insert="true"
            column="location"
        />

        <property
            name="managerNumber"
            type="java.lang.String"
            update="true"
            insert="true"
            column="manager_sas_empno"
        />

        <property
            name="outLookSyncStatus"
            type="java.lang.String"
            update="true"
            insert="true"
            column="outlooksynchstatus"
        />

        <property
            name="phone"
            type="java.lang.String"
            update="true"
            insert="true"
            column="phone"
        />

        <property
            name="region"
            type="java.lang.String"
            update="true"
            insert="true"
            column="region"
        />

        <property
            name="room"
            type="java.lang.String"
            update="true"
            insert="true"
            column="room"
        />

        <many-to-one
            name="businessUnit"
            class="com.sas.estars.framework.businessunit.BusinessUnitImpl"
            cascade="none"
            fetch="join"
            update="true"
            insert="true"
            column="business_unit_id"
        />

        <many-to-one
            name="territory"
            class="com.sas.estars.framework.customization.lov.ListValueImpl"
            cascade="none"
            fetch="join"
            update="true"
            insert="true"
            column="territory_lov_id"
        />

        <property
            name="unit"
            type="java.lang.String"
            update="true"
            insert="true"
            column="unit"
        />

        <property
            name="legacyEmployeeNumber"
            type="java.lang.String"
            update="true"
            insert="true"
            column="legacy_empno"
        />

        <bag
            name="contacts"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.person.contact.ContactEmployeeImpl"
            />

      </bag>

        <bag
            name="opportunities"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.opportunity.OpportunityEmployeeImpl"
            />

      </bag>

        <bag
            name="accounts"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.account.AccountEmployeeImpl"
            />

      </bag>

        <bag
            name="references"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.reference.ReferenceEmployeeImpl"
            />

      </bag>

        <bag
            name="events"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.event.EventEmployeeImpl"
            />

      </bag>

        <bag
            name="comits"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.comit.ComitEmployeeImpl"
            />

      </bag>

        <bag
            name="desktops"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="all-delete-orphan"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.person.employee.desktop.EmployeeDesktopImpl"
            />

      </bag>

        <component
            name="matchCodeHolder"
            class="com.sas.estars.framework.person.employee.EmployeeMatchCodeHolder"
        >
        <property
            name="firstNameMatchCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="first_name_matchcode"
        />

        <property
            name="lastNameMatchCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="last_name_matchcode"
        />

        <property
            name="nameMatchCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="nameMatchCode"
        />

        </component>

        <bag
            name="notes"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="save-update, merge"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.person.employee.EmployeeNoteImpl"
            />

      </bag>

        <bag
            name="employeeRoles"
            optimistic-lock="false"
            lazy="true"
            inverse="true"
            cascade="all-delete-orphan"
        >

            <key
                column="employee_id"
            >
            </key>

            <one-to-many
                  class="com.sas.estars.framework.security.EmployeeRoleImpl"
            />

      </bag>

        <component
            name="auditInfo"
            class="com.sas.estars.framework.audit.AuditInfo"
        >

        <many-to-one
            name="createdBy"
            class="com.sas.estars.framework.person.employee.EmployeeImpl"
            cascade="none"
            fetch="select"
            update="true"
            insert="true"
            column="createdBy"
        />

        <many-to-one
            name="updatedBy"
            class="com.sas.estars.framework.person.employee.EmployeeImpl"
            cascade="none"
            fetch="select"
            update="true"
            insert="true"
            column="updatedBy"
        />

        <property
            name="created"
            type="java.util.Date"
            update="true"
            insert="true"
            column="created"
        />

        <property
            name="lastUpdated"
            type="java.util.Date"
            update="true"
            insert="true"
            column="updated"
        />

        </component>

        </component>

<!--    <!-- one-to-zero-or-one mapping -->
        <component name="employeeInfo"
           class="com.sas.estars.framework.person.employee.EmployeeInfoImpl"
           fetch="select"
          update="true"
           unique="true"
           lazy="no-proxy"
        />-->

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 10:23 am 
Newbie

Joined: Fri Oct 27, 2006 11:39 am
Posts: 10
Meant to add.... The app works just fine, it just doesn't lazy-load the component, it retrieves the whole thing every time... My other option is to create a one-one mapping, using a View to expose the basics, and the tables for the extra stuff... but then I've got to test out the saving functionality... should just be as simple as a cascade="all", but I don't know if Hibernate/Oracle will have a problem with updating the same table twice... Should be ok in the same transaction...


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