Hibernate version: 2.1.2
Mapping documents:
CategoryBean
<hibernate-mapping>
<class name="com.elendal.jFF.model.CategoryBean" lazy="true" table="category">
<id name="id" type="int">
<generator class="native"/>
</id>
<property name="title" column="title" type="string" not-null="true"/>
<property name="description" column="description" type="string" not-null="true"/>
<set name="boards" table="board">
<key column="cat_id"/>
<one-to-many class="com.elendal.jFF.model.BoardBean"/>
</set>
</class>
</hibernate-mapping>
BoardBean
<hibernate-mapping>
<class name="com.elendal.jFF.model.BoardBean" lazy="true" table="board">
<id column="id" type="int">
<generator class="native"/>
</id>
<property name="num_threads" column="num_threads" type="int" not-null="true"/>
<property name="title" column="title" type="string" not-null="true"/>
<property name="description" column="description" type="string" not-null="true"/>
<many-to-one name="category" class="com.elendal.jFF.model.CategoryBean" column="cat_id"/>
<set name="threads" table="THREAD_GROUP" lazy="true">
<key column="board_id"/>
<one-to-many class="com.elendal.jFF.model.ThreadGroupBean"/>
</set>
</class>
</hibernate-mapping>
Java Code small sample
List categories;
categories = session.createCriteria(CategoryBean.class)
.setFirstResult(0)
.setMaxResults(10)
.list();
CategoryBean categoryBean = (CategoryBean) (categories.toArray()[0]);
BoardBean bb =(BoardBean)categoryBean.getBoards().toArray()[0];
logger.info(new Integer(bb.getId()));
Database: Oracle 9i
Hibernate log
2004-02-20 20:12:44,371 DEBUG net.sf.hibernate.impl.SessionImpl.<init>(SessionImpl.java:531) : opened session
2004-02-20 20:12:44,380 DEBUG net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2210) : flushing session
2004-02-20 20:12:44,382 DEBUG net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2403) : Flushing entities and processing referenced collections
2004-02-20 20:12:44,384 DEBUG net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2746) : Processing unreferenced collections
2004-02-20 20:12:44,386 DEBUG net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2760) : Scheduling collection removes/(re)creates/updates
2004-02-20 20:12:44,387 DEBUG net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2234) : Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-02-20 20:12:44,389 DEBUG net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2239) : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-02-20 20:12:44,391 DEBUG net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1782) : Dont need to execute flush
2004-02-20 20:12:44,393 DEBUG net.sf.hibernate.impl.BatcherImpl.logOpenPreparedStatement(BatcherImpl.java:196) : about to open: 0 open PreparedStatements, 0 open ResultSets
2004-02-20 20:12:44,436 DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:78) : total checked-out connections:
0
2004-02-20 20:12:44,441 DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:84) : using pooled JDBC connection,
pool size: 0
2004-02-20 20:12:44,688 DEBUG net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:237) : select * from ( select this.id as id0_, this.title as title0_, this.
description as descript3_0_ from category this where 1=1 ) where rownum <= ?
2004-02-20 20:12:44,690 DEBUG net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:241) : preparing statement
2004-02-20 20:12:44,704 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:197) : processing result set
2004-02-20 20:12:44,708 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '4' as column: id0_
2004-02-20 20:12:44,710 DEBUG net.sf.hibernate.loader.Loader.getRow(Loader.java:405) : result row: 4
2004-02-20 20:12:44,713 DEBUG net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:536) : Initializing object from ResultSet: 4
2004-02-20 20:12:44,716 DEBUG net.sf.hibernate.loader.Loader.hydrate(Loader.java:605) : Hydrating entity: com.elendal.jFF.model.CategoryBean#4
2004-02-20 20:12:44,718 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning 'Java world' as column: title0_
2004-02-20 20:12:44,732 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning 'Place where programers make magic' as column: descript3_0_
2004-02-20 20:12:44,735 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:226) : done processing result set (1 rows)
2004-02-20 20:12:44,740 DEBUG net.sf.hibernate.impl.BatcherImpl.logClosePreparedStatement(BatcherImpl.java:203) : done closing: 0 open PreparedStatements, 0 open ResultSets
2004-02-20 20:12:44,743 DEBUG net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:261) : closing statement
2004-02-20 20:12:44,747 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:239) : total objects hydrated: 1
2004-02-20 20:12:44,749 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2166) : resolving associations for [com.elendal.jFF.model.Category
Bean#4]
2004-02-20 20:12:44,752 DEBUG net.sf.hibernate.impl.SessionImpl.getCollection(SessionImpl.java:3891) : creating collection wrapper:[com.elendal.jFF.model.CategoryBe
an.boards#4]
2004-02-20 20:12:44,755 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2182) : calling onLoad()
2004-02-20 20:12:44,757 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2190) : done materializing entity [com.elendal.jFF.model.CategoryB
ean#4]
2004-02-20 20:12:44,759 DEBUG net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3082) : initializing non-lazy collections
2004-02-20 20:12:44,761 DEBUG net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3218) : initializing collection [com.elendal.jFF.model.Categor
yBean.boards#4]
2004-02-20 20:12:44,767 DEBUG net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3219) : checking second-level cache
2004-02-20 20:12:44,776 DEBUG net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3225) : collection not cached
2004-02-20 20:12:44,780 DEBUG net.sf.hibernate.impl.BatcherImpl.logOpenPreparedStatement(BatcherImpl.java:196) : about to open: 0 open PreparedStatements, 0 open ResultSets
2004-02-20 20:12:44,782 DEBUG net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:237) : select boards0_.id as id__, boards0_.cat_id as cat_id__, boards0_.id
as id0_, boards0_.num_threads as num_thre2_0_, boards0_.title as title0_, boards0_.description as descript4_0_, boards0_.cat_id as cat_id0_ from board boards0_ where boards0_.c
at_id=?
2004-02-20 20:12:44,784 DEBUG net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:241) : preparing statement
2004-02-20 20:12:44,789 DEBUG net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:46) : binding '4' to parameter: 1
2004-02-20 20:12:44,806 DEBUG net.sf.hibernate.loader.Loader.handleEmptyCollections(Loader.java:327) : result set contains (possibly empty) collection: [com.elendal.jFF.model.CategoryBean.boards#4]
2004-02-20 20:12:44,808 DEBUG net.sf.hibernate.impl.SessionImpl.getLoadingCollection(SessionImpl.java:2984) : uninitialized collection: initializing
2004-02-20 20:12:44,812 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:197) : processing result set
2004-02-20 20:12:44,814 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '5' as column: id0_
2004-02-20 20:12:44,816 DEBUG net.sf.hibernate.loader.Loader.getRow(Loader.java:405) : result row: 5
2004-02-20 20:12:44,818 DEBUG net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:536) : Initializing object from ResultSet: 5
2004-02-20 20:12:44,820 DEBUG net.sf.hibernate.loader.Loader.hydrate(Loader.java:605) : Hydrating entity: com.elendal.jFF.model.BoardBean#5
2004-02-20 20:12:44,827 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '3' as column: num_thre2_0_
2004-02-20 20:12:44,830 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning 'J2SE' as column: title0_
2004-02-20 20:12:44,833 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning 'Write once debug everywere' as column: descript4_0_
2004-02-20 20:12:44,835 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '4' as column: cat_id0_
2004-02-20 20:12:44,841 DEBUG net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '4' as column: cat_id__
2004-02-20 20:12:44,843 DEBUG net.sf.hibernate.loader.Loader.readCollectionElement(Loader.java:292) : found row of collection: [com.elendal.jFF.model.CategoryBean.b
oards#4]
2004-02-20 20:12:44,845 DEBUG net.sf.hibernate.impl.SessionImpl.getLoadingCollection(SessionImpl.java:3007) : reading row
2004-02-20 20:12:44,847 DEBUG
net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:68) : returning '5' as column: id__
2004-02-20 20:12:44,849 DEBUG net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1950) : loading [com.elendal.jFF.model.BoardBean#5]
2004-02-20 20:12:44,864 DEBUG net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2047) : attempting to resolve [com.elendal.jFF.model.BoardBean#5]
2004-02-20 20:12:44,868 DEBUG net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2063) : resolved object in session cache [com.elendal.jFF.model.BoardBean#5]
2004-02-20 20:12:44,871 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:226) : done processing result set (1 rows)
2004-02-20 20:12:44,874 DEBUG net.sf.hibernate.impl.BatcherImpl.logClosePreparedStatement(BatcherImpl.java:203) : done closing: 0 open PreparedStatements, 0 open ResultSets
2004-02-20 20:12:44,877 DEBUG net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:261) : closing statement
2004-02-20 20:12:44,880 DEBUG net.sf.hibernate.loader.Loader.doQuery(Loader.java:239) : total objects hydrated: 1
2004-02-20 20:12:44,884 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2166) : resolving associations for [com.elendal.jFF.model.BoardBea
n#5]
2004-02-20 20:12:44,886 DEBUG net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1950) : loading [com.elendal.jFF.model.CategoryBean#4]
2004-02-20 20:12:44,888 DEBUG net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2047) : attempting to resolve [com.elendal.jFF.model.CategoryBean#4]
2004-02-20 20:12:44,889 DEBUG net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2063) : resolved object in session cache [com.elendal.jFF.model.CategoryBean
#4]
2004-02-20 20:12:44,894 DEBUG net.sf.hibernate.impl.SessionImpl.getCollection(SessionImpl.java:3891) : creating collection wrapper:[com.elendal.jFF.model.BoardBean.
threads#5]
2004-02-20 20:12:44,896 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2182) : calling onLoad()
2004-02-20 20:12:44,898 DEBUG net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2190) : done materializing entity [com.elendal.jFF.model.BoardBean
#5]
2004-02-20 20:12:44,900 DEBUG net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3043) : 1 collections were found in result set
2004-02-20 20:12:44,902 DEBUG net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3061) : collection fully initialized: [com.elendal.jFF.model.
CategoryBean.boards#4]
2004-02-20 20:12:44,903 DEBUG net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3064) : 1 collections initialized
2004-02-20 20:12:44,918 DEBUG net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3227) : collection initialized
2004-02-20 20:12:44,921 INFO
com.elendal.jFF.servlets.IndexServlet.service(IndexServlet.java:68) : 0
2004-02-20 20:12:44,923 DEBUG net.sf.hibernate.impl.SessionImpl.close(SessionImpl.java:549) : closing session
2004-02-20 20:12:44,926 DEBUG net.sf.hibernate.impl.SessionImpl.disconnect(SessionImpl.java:3294) : disconnecting session
2004-02-20 20:12:44,928 DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider.closeConnection(DriverManagerConnectionProvider.java:114) : returning connection to pool, pool size: 1
2004-02-20 20:12:44,931 DEBUG net.sf.hibernate.impl.SessionImpl.afterTransactionCompletion(SessionImpl.java:561): transaction completion
My question is who category.id is 0 and not 5?
And why this query selects same information 2 times? (probably because it is bi-directional...)
select boards0_.id as id__, boards0_.cat_id as cat_id__, boards0_.id
as id0_, boards0_.num_threads as num_thre2_0_, boards0_.title as title0_, boards0_.description as descript4_0_, boards0_.cat_id as cat_id0_ from board boards0_ where boards0_.c
at_id=?
_________________ When nothing can possibly go wrong, it will.
|