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