-->
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: Help!a problem occurs when retrieving a collection
PostPosted: Wed May 11, 2005 10:28 am 
Newbie

Joined: Sun May 08, 2005 11:46 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Please help me solve this problem.
I recently participated in a project.I use hibernate as it is a very excellent
option in handling persistence layer.However,I encountered a problem whcih has already bothered me for two days.Here is my problem:
The project is developing an online test system.The database system I use is Oracle 9i.I created two tables,one is TESTRECORD and the other is
REVIEW.Scriplets are:
TESTRECORD table
CREATE TABLE TESTRECORD
(
T_ID VARCHAR2(64 BYTE) NOT NULL,
SUB_ID VARCHAR2(64 BYTE) NOT NULL,
T_SCORE NUMBER(38) DEFAULT 0,
T_DURATION NUMBER(38),
STUDENT_ID NUMBER(38) NOT NULL,
RV_OPTION NUMBER(3) DEFAULT 0 NOT NULL,
RV_TEACHER_ID NUMBER(38),
CREATE_TIME TIMESTAMP(6) NOT NULL,
LAST_UPDATE_TIME TIMESTAMP(6)
)
CREATE UNIQUE INDEX PK8 ON TESTRECORD
(T_ID)
ALTER TABLE TESTRECORD ADD (
CONSTRAINT PK8 PRIMARY KEY (T_ID)

REVIEW table:
CREATE TABLE REVIEW
(
RV_ID VARCHAR2(64 BYTE) NOT NULL,
T_ID VARCHAR2(64 BYTE) NOT NULL,
RV_CONTENT VARCHAR2(2000 BYTE) NOT NULL,
TEACHER_ID NUMBER(38),
CREATE_TIME TIMESTAMP(6) NOT NULL,
LAST_UPDATE_TIME TIMESTAMP(6)
)
CREATE UNIQUE INDEX PK9 ON REVIEW
(RV_ID)
CREATE INDEX REVIEW_TESTRECORD ON REVIEW
(T_ID)
ALTER TABLE REVIEW ADD (
CONSTRAINT PK9 PRIMARY KEY (RV_ID)

ALTER TABLE REVIEW ADD (
CONSTRAINT REVIEW_TESTRECORD FOREIGN KEY (T_ID)
REFERENCES TESTRECORD (T_ID)
ON DELETE CASCADE);
Hibernate version:
tomcat+hibernate-2.1
Mapping documents:
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>

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

<class name="TestRecord" table="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"
cascade="all-delete-orphan">
<key column="T_ID" />
<one-to-many class="Review" />
</set>


</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
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:343)
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

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I hope anybody can help me conquer it.Thank you.
Best Regards!


Top
 Profile  
 
 Post subject: Supplementary
PostPosted: Wed May 11, 2005 11:39 am 
Newbie

Joined: Sun May 08, 2005 11:46 am
Posts: 3
I also use struts as the framework of the project
Next are some of the classes I use
NeGridTestManager.java (class that wrap the hibernate properties and provides interfaces for users to manipulate database)
/*
* retrieve review records according to the T_ID foreign key
* @param testRecord
*/
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
/*
testRecord=(TestRecord)session.load(TestRecord.class,id);
if(!Hibernate.isInitialized(testRecord.getReviews()))
Hibernate.initialize(testRecord.getReviews());
*/
session.lock(testRecord,LockMode.READ);
if(!Hibernate.isInitialized(testRecord.getReviews()))
Hibernate.initialize(testRecord.getReviews());
reviews=testRecord.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]);
}
TestReviewAction.java(the action class )
public class TestReviewAction extends DispatchAction{
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward getReviews(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestReviewForm testReviewForm=(TestReviewForm)form;
HttpSession session=request.getSession();
testReviewForm=(TestReviewForm)session.getAttribute("testReviewForm");
int index=Integer.valueOf(request.getParameter("index")).intValue();
TestRecord[] testRecords=testReviewForm.getTestRecords();
TestRecord testRecord=testRecords[index];
request.setAttribute("reviews",NeGridTestManager.getReviews(testRecord));
return mapping.findForward("showReviews");
}
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward getTestRecords(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestReviewForm testReviewForm;
if(form==null)
testReviewForm=new TestReviewForm();
else
testReviewForm=(TestReviewForm)form;
long studentId=testReviewForm.getStudentId().longValue();//it receives a request parameter from a jsp form submit
TestRecord[] testRecords=NeGridTestManager.getTestRecordsByStudentId(studentId);
testReviewForm.setTestRecords(testRecords);
request.setAttribute("id",testReviewForm.getStudentId());
return mapping.findForward("showTestRecords");
}


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.