Hi,
i´m in serious trouble understanding the one-to-one relationship in Hibernate. I´ve got two simple tables like this:
Code:
_____________ __________
| | | |
| Einrichtung | | Adresse |
|_____________| |_________|
| | | |
| id | ----- | id |
|_____________| | |_________|
| | | | |
| name | | | street |
|_____________| | |_________|
| | | | |
| adressID |<--- | ... |
|_____________| |_________|
(Einrichtung is a german word, translate it with 'institution')
I´m trying hard to generate a mapping for this simple relation, without luck yet.
Hibernate version:2.7.1 Mapping documents:Code:
<hibernate-mapping>
<class name="myParentClass" table="ParentClass" dynamic-insert="true" dynamic-update="true" select-before-update="true" optimistic-lock="version">
<id name="id" type="integer" unsaved-value="null">
<column name="id" sql-type="integer" not-null="true"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="name" length="255" not-null="false"/>
</property>
<one-to-one name="id" class="de.hannit.fsch.benutzerverwaltung.Adresse" cascade="none" />
</class>
<!-- Mapping für die Adresse -->
<class name="de.hannit.fsch.benutzerverwaltung.Adresse" table="Adresse" dynamic-insert="true" dynamic-update="true" select-before-update="true" optimistic-lock="version">
<!-- Primärschlüssel für die Adresse -->
<id name="id" type="integer" unsaved-value="null">
<column name="id" sql-type="integer" not-null="true"/>
<generator class="identity"/>
</id>
<property name="strasse">
<column name="strasse" not-null="false"/>
</property>
.
. other properties
.
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
einrichtung = (Einrichtung) load(new Integer(iID));
(iID ==6)
Here is what Hibernate logs:
Code:
select einrichtun0_.id as id0_, einrichtun0_.name as name0_ from Einrichtung einrichtun0_ where einrichtun0_.id=?
Hibernate: select einrichtun0_.id as id0_, einrichtun0_.name as name0_ from Einrichtung einrichtun0_ where einrichtun0_.id=?
net.sf.hibernate.impl.BatcherImpl - preparing statement
DEBUG net.sf.hibernate.type.IntegerType - binding '6' to parameter: 1
DEBUG net.sf.hibernate.loader.Loader - processing result set
DEBUG net.sf.hibernate.loader.Loader - result row: 6
DEBUG net.sf.hibernate.loader.Loader - Initializing object from ResultSet: 6
DEBUG net.sf.hibernate.loader.Loader - Hydrating entity: de.hannit.fsch.benutzerverwaltung.Einrichtung#6
DEBUG net.sf.hibernate.type.StringType - returning 'Pflegeheim Erdmöbel ' as column: name0_
DEBUG net.sf.hibernate.loader.Loader - done processing result set (1 rows)
DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
DEBUG net.sf.hibernate.loader.Loader - total objects hydrated: 1
so far, so good. But now, hibernate tries to load the adress object with using the same id ! (6)Code:
DEBUG net.sf.hibernate.impl.SessionImpl - resolving associations for [de.hannit.fsch.benutzerverwaltung.Einrichtung#6]
DEBUG net.sf.hibernate.impl.SessionImpl - loading [de.hannit.fsch.benutzerverwaltung.Adresse#6]
DEBUG net.sf.hibernate.impl.SessionImpl - attempting to resolve [de.hannit.fsch.benutzerverwaltung.Adresse#6]
DEBUG net.sf.hibernate.impl.SessionImpl - object not resolved in any cache [de.hannit.fsch.benutzerverwaltung.Adresse#6]
DEBUG net.sf.hibernate.persister.EntityPersister - Materializing entity: [de.hannit.fsch.benutzerverwaltung.Adresse#6]
DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG net.sf.hibernate.SQL - select adresse0_.id as id0_, adresse0_.strasse as strasse0_, adresse0_.hausnummer as hausnummer0_, adresse0_.zusatz as zusatz0_, adresse0_.postleitzahl as postleit5_0_, adresse0_.ort as ort0_ from Adresse adresse0_ where adresse0_.id=?
Hibernate: select adresse0_.id as id0_, adresse0_.strasse as strasse0_, adresse0_.hausnummer as hausnummer0_, adresse0_.zusatz as zusatz0_, adresse0_.postleitzahl as postleit5_0_, adresse0_.ort as ort0_ from Adresse adresse0_ where adresse0_.id=?
DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
DEBUG net.sf.hibernate.type.IntegerType - binding '6' to parameter: 1
DEBUG net.sf.hibernate.loader.Loader - processing result set
DEBUG net.sf.hibernate.loader.Loader - done processing result set (0 rows)
DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
DEBUG net.sf.hibernate.loader.Loader - total objects hydrated: 0
I am totally confused. Any suggestions for me ?
Name and version of the database you are using:MS SQL Server 2000
The generated SQL (show_sql=true):see log