-->
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.  [ 2 posts ] 
Author Message
 Post subject: One to one mapping confusion
PostPosted: Wed Jul 26, 2006 11:31 am 
Newbie

Joined: Thu Jul 13, 2006 8:19 pm
Posts: 3
Greetings,

I have two classes, and two corresponding tables: User and Credentials. A User should have only one Credentials and vice-versa.

I have read the Hibernate literature on one-to-one mappings, but I believe I am missing something. With the mappings below, I can get a Credentials object which can then get its corresponding User object, but the User object thinks that its Credentials object is null. So I have currently got squirrelly code that goes:
Code:
List creds = getHibernateTemplate().find("from Credentials where username=?", username);
Credentials cred = (Credentials)creds.get(0);
user = cred.getUser();
user.setCredentials(cred);


Obviously this is not ideal and I believe the issue is that I have problems with my mapping documents.

Hibernate version: 3.1.3

Mapping documents:
User 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>

    <class name="com.acme.model.User"
            table="contact"
            lazy="false">

    <id name="id" column="contact_id" unsaved-value="0">
    <generator class="sequence">
        <param name="sequence">
            seq_contact_id
        </param>
    </generator>
    </id>

        <version
            name="version"
            column="version"
            type="java.lang.Integer"
        />

        <one-to-one
            name="credentials"
            class="com.acme.model.Credentials"
            cascade="all">
        </one-to-one>

        <property
            name="firstName"
            column="fname"
            length="100"
            not-null="true">
        </property>

        <property
            name="lastName"
            column="lname"
            length="100"
            not-null="true">
        </property>

        <property
            name="passwordHint"
            column="password_hint"
            not-null="false">
        </property>

        <set
            name="roles"
            table="contact_role"
            lazy="false"
            cascade="save-update"
        >

            <key column="contact_id">
            </key>

            <many-to-many
                class="com.acme.model.Role"
                column="role_id"
                outer-join="auto"
             />

        </set>

        <property
            name="enabled"
            type="boolean"
            column="active">
        </property>

        <property
            name="accountExpired"
            type="boolean"
            column="account_expired"
            not-null="true">
        </property>

        <property
            name="accountLocked"
            type="boolean"
            column="account_locked"
            not-null="true">
        </property>

        <property
            name="credentialsExpired"
            type="boolean"
            column="credentials_expired"
            not-null="true">
        </property>

        <property
            name="alxId"
            column="alx_id"
            length="20">
        </property>

        <component name="history">

        <many-to-one
            name="createdBy"
            class="com.acme.model.AcmeUser"
            column="creator_id">
        </many-to-one>

        <many-to-one
            name="modifiedBy"
            class="com.acme.model.AcmeUser"
            column="modifier_id">
        </many-to-one>

        </component>

        <property
            name="middleName"
            column="mname"
            length="100">
        </property>

        <many-to-one
            name="prefix"
            class="com.acme.model.component.Prefix"
            column="prefix_id">

        </many-to-one>

        <many-to-one
            name="suffix"
            class="com.acme.model.component.Suffix"
            column="suffix_id"
            cascade="none">
        </many-to-one>

        <many-to-one
            name="sourceType"
            class="com.acme.model.component.ContactSourceType"
            column="source_type_id">
        </many-to-one>

        <property
            name="dateCreated"
            type="java.util.Date"
            column="date_created">
        </property>

        <property
            name="dateModified"
            type="java.util.Date"
            column="date_modified">
        </property>

    </class>   

</hibernate-mapping>


Credentials 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>

    <class name="com.acme.model.Credentials" table="contact_credentials" lazy="false">

    <id name="id" column="credentials_id" unsaved-value="0">
    <generator class="sequence">
        <param name="sequence">seq_credentials_id</param>
    </generator>
    </id>

        <many-to-one
            name="user"
            class="com.acme.model.User"
            column="contact_id"
            not-null="true"
            unique="true">
        </many-to-one>

        <property
            name="userName"
            column="username"
            length="100"
            not-null="true">
        </property>

        <property
            name="userPassword"
            column="user_password"
            length="255"
            not-null="true">
        </property>

    </class>   

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
(None, using AppFuse)

Full stack trace of any exception that occurs:
(N/A... although if I remove lazy="false" from the mappings I get:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed.
I can create that error and post the trace if it would be helpful.)

Name and version of the database you are using: Postgresql 8.1

The generated SQL (show_sql=true):
I have show_sql set to true, but it's not generating any in the console or logs.

Debug level Hibernate log excerpt: N/a


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 11:53 am 
Newbie

Joined: Fri Jun 09, 2006 8:03 am
Posts: 12
The lazy is not a problem with your mapping. It is a problem with lazy initialization. Hibernate doesn't load an association until you need to use it. However, if the session is closed, Hibernate can no longer retrieve the assoctiation. This is what has happened to you, you closed the session to early.


As for your null, the one-to-one needs to have a property-ref point to the property you are mapping too, not sure which one you want but like the example in hibernate has :

<one-to-one name="person"
property-ref="address"/>

so you might want "user"


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