-->
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: Problem in using Hibernate.initilize()
PostPosted: Thu May 12, 2005 9:35 am 
Newbie

Joined: Sun May 08, 2005 11:46 am
Posts: 3
I recently participated in a project which uses Hibernate as its persistence
l,ayer manager.However,I encouter a problem when retrieving a collection
property.Mapping files are as follows:
TestRecord.hbm.xml
<hibernate-mapping package="com.negrid.onlinetest.hibernate.persistence">

<class name="TestRecord" table="TESTRECORD" proxy="TestRecord">
<id name="id" column="T_ID" type="java.lang.String" unsaved-value="id-value">
<generator class="assigned"/>
</id>

<property name="score" column="T_SCORE" type="java.lang.Long" />
<property name="time" column="T_DURATION" type="java.lang.Long" />
<property name="studentId" column="STUDENT_ID" type="java.lang.Long" not-null="true" />
<property name="rvOption" column="RV_OPTION" type="java.lang.Short" />
<property name="teacherId" column="RV_TEACHER_ID" type="java.lang.Long" />
<property name="createTime" column="CREATE_TIME" type="java.util.Date" not-null="true" />
<property name="lastUpdateTime" column="LAST_UPDATE_TIME" type="java.util.Date" />

<set name="reviews"
order-by="RV_ID asc"
lazy="true"
inverse="true"
batch-size="12"
cascade="all-delete-orphan">
<key column="T_ID" />
<one-to-many class="Review" />
</set>

<many-to-one name="subject" column="SUB_ID" class="Subject" update="false"/>
</class>

</hibernate-mapping>

Review.hbm.xml
<hibernate-mapping package="com.negrid.onlinetest.hibernate.persistence">

<class name="Review" table="REVIEW">
<id name="id" column="RV_ID " type="java.lang.String">
<generator class="assigned"/>
</id>

<property name="content" column="RV_CONTENT" type="java.lang.String" not-null="true" />
<property name="teacherId" column="TEACHER_ID" type="java.lang.Long" />
<property name="createTime" column="CREATE_TIME" type="java.util.Date" not-null="true" />
<property name="lastUpdateTime" column="LAST_UPDATE_TIME" type="java.util.Date" />

<many-to-one name="testRecord" column="T_ID" class="TestRecord" not-null="true" cascade="all" />
</class>

</hibernate-mapping>

in a method I pass a detached object and use Hibernate.initilize()
to reattach itself and its collection property.Code is as follows:
public static Review[] getReviews(TestRecord testRecord){
Session session=null;
Transaction tx=null;
Set reviews=null;
try{
session=HibernateSessionFactory.currentSession(); //create hibernate session
tx=session.beginTransaction();//begin transaction
session.lock(testRecord,LockMode.READ);
TestRecord record=(TestRecord)session.load(TestRecord.class,testRecord.getId());
if(!Hibernate.isInitialized(record.getReviews()))
Hibernate.initialize(record.getReviews());
reviews=record.getReviews();
tx.commit();
session.close();
} catch(HibernateException e){
e.printStackTrace();
//if an error occurs,better to rollback transaction
try{
if(tx!=null)
tx.rollback();
}catch(HibernateException e1){
e1.printStackTrace();
}
}finally{
try{
if(session!=null)
session.close();
}catch(HibernateException e2){
e2.printStackTrace();
}
}
return (Review[]) reviews.toArray(new Review[0]);
}

Note:The parameter testRecord is a record previously got from the
database and I made some changes in the web tier.Then I want to
retrieve its collection property which is defined as (Set reviews=new HashSet()).I use Hibernate.initilize() and then an exception occurs.
P13_0_ from SUBJECT subject0_ where subject0_.SUB_ID=?

Hibernate: select reviews0_.T_ID as T_ID__, reviews0_.RV_ID as RV_ID __, review
s0_.RV_ID as RV_ID 0_, reviews0_.RV_CONTENT as RV_CONTENT0_, reviews0_.TEACHER_
ID as TEACHER_ID0_, reviews0_.CREATE_TIME as CREATE_T4_0_, reviews0_.LAST_UPDATE
_TIME as LAST_UPD5_0_, reviews0_.T_ID as T_ID0_ from REVIEW reviews0_ where revi
ews0_.T_ID=? order by reviews0_.RV_ID asc
net.sf.hibernate.exception.GenericJDBCException: could not initialize collection
: [com.negrid.onlinetest.hibernate.persistence.TestRecord.reviews#UESTC50]
at net.sf.hibernate.exception.ErrorCodeConverter.handledNonSpecificExcep
tion(ErrorCodeConverter.java:90)
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConver
ter.java:79)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionH
elper.java:30)
at net.sf.hibernate.collection.AbstractCollectionPersister.convert(Abstr
actCollectionPersister.java:728)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(Ab
stractCollectionPersister.java:291)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.ja
va:3303)
at net.sf.hibernate.collection.PersistentCollection.forceInitialization(
PersistentCollection.java:336)
at net.sf.hibernate.Hibernate.initialize(Hibernate.java:256)
at com.negrid.onlinetest.db.manager.NeGridTestManager.getReviews(NeGridT
estManager.java:339)
at com.negrid.onlinetest.struts.action.TestReviewAction.getReviews(TestR
eviewAction.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchActio
n.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:
216)
at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:148
2)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at com.negrid.onlinetest.struts.filters.SetCharacterEncodingFilter.doFil
ter(SetCharacterEncodingFilter.java:97)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at com.negrid.onlinetest.struts.filters.SetCharacterEncodingFilter.doFil
ter(SetCharacterEncodingFilter.java:97)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: ORA-00911: invalid character

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.
java:181)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_describe(T4CPrepa
redStatement.java:420)
at oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleState
ment.java:896)
at oracle.jdbc.driver.T4CPreparedStatement.execute_maybe_describe(T4CPre
paredStatement.java:452)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStateme
nt.java:986)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePrep
aredStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePrepare
dStatement.java:2929)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections
(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.ja
va:93)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(Ab
stractCollectionPersister.java:288)
... 47 more
Please help me!


Top
 Profile  
 
 Post subject: Re: Problem in using Hibernate.initilize()
PostPosted: Thu May 12, 2005 5:30 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
You need to initialize the collection prior to passing the data to the web tier. Or re-associate the object with a new Session and then access it.

Once you close the hiberante Session you used to load the object. The Hibernate.initialize() won't help. (It's purpose is to initialize a collection without having to traverse the collection)


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.