论坛首页 Java企业应用论坛

hibernate连oracle的问题

浏览 4311 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-02-20  
我有2个类CaseWork和WorkAttachment是一对多的关系.
CaseWork.hbm.xml中的配置为:
         <set
            name="attachmentSet"
            table="work_attachment"
>

              <key
                  column="case_work_id"
              />
             <one-to-many
                  class="com.raykey.netactivity.front.model.WorkAttachment"
              />             
        </set>


-------------------------------
WorkAttachment.hbm.xml的配置为:
        <many-to-one
            name="caseWork"
            class="com.raykey.netactivity.front.model.CaseWork" 
            column="case_work_id"
/>

我的查询语句:List list = session.find("from CaseWork a where a.workName = ?", name, Hibernate.STRING);

问题就来了:在oracle中查询CaseWork的时候,必须加上Lazy= true才可以正常执行;否则会报错误信息:
net.sf.hibernate.JDBCException: could not initialize collection: [com.raykey.netactivity.front.model.CaseWork.attachmentSet#4028818bfad0d2dd00fad0d2e3150001]

而在mysql中查询是完全没有问题的,请问这是什么原因?
谢谢大家了.
   发表时间:2004-02-20  
你没加外键(foreign key)吧
0 请登录后投票
   发表时间:2004-02-20  
case_work_id这个不就是外键么?
0 请登录后投票
   发表时间:2004-02-20  
你在数据库里把这个设为外键了吗?
0 请登录后投票
   发表时间:2004-02-20  
对呀.我把完整的代码贴出来好了.
CaseWork类中的元素:
public class CaseWork {
private    String workID; // id
private    String authorName; // 作者名
private    Set attachmentSet; // 有多个附件

}

CaseWork的映射文件:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.raykey.netactivity.front.model.CaseWork"
        table="case_work"
    >
        <id
            name="workID"
            column="case_work_id"
        >
            <generator class="uuid.hex"/>
        </id>
       
<property
            name="authorName"
            type="string"
            column="author_name"
        />
       
<set
            name="attachmentSet"
            table="work_attachment"
            cascade="none"
>

              <key
                  column="case_work_id"
              />
             <one-to-many
                  class="com.raykey.netactivity.front.model.WorkAttachment"
              />             
        </set>

     </class>
</hibernate-mapping>


WorkAttachment类的元素:

public class WorkAttachment {
    private    String fileName; // 附件路径名称,在服务器上的地址,唯一标志
    private    String originalName; // 原始名称

    private    CaseWork caseWork; // 一对多的关系
}

WorkAttachment类的映射文件:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.raykey.netactivity.front.model.WorkAttachment"
        table="work_attachment"
    >
        <id
            name="fileName"
            column="file_name"
            unsaved-value="null"
        >
            <generator class="assigned"/>
        </id>
       
<property
            name="originalName"
            type="string"
            update="true"
            insert="true"
            column="original_name"
        />

        <many-to-one
            name="caseWork"
            class="com.raykey.netactivity.front.model.CaseWork" 
            column="case_work_id"
/>

     </class>
</hibernate-mapping>


执行的查询语句是这样的:
List list = session.find("from CaseWork as a where a.authorName = ?", name, Hibernate.STRING);

在mysql中可以查找出casework和它下面的集合AttachmentSet

而连到oracle数据库时,就会报错:
net.sf.hibernate.JDBCException: could not initialize collection:

[com.raykey.netactivity.front.model.CaseWork.attachmentSet#4028818bfad0fd0c00fad0fd11f80001]
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:289)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3128)
at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:331)
at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3002)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.list(Loader.java:918)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:992)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1470)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1449)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1445)
at com.raykey.netactivity.front.model.handler.impl.CaseWorkHandlerImpl.findWorkByName(CaseWorkHandlerImpl.java:37)
at test.com.raykey.netactivity.front.testModelCaseWork.testfindWorkByName(testModelCaseWork.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
Caused by: java.sql.SQLException: ORA-00933: SQL ???????

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:169)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1405)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:643)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:1819)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2015)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:395)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:339)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:790)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:184)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:128)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:905)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:880)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:79)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:286)
... 26 more
0 请登录后投票
   发表时间:2004-02-20  
我试过,好像没什么问题嘛

尽量别用assigned,还有双向关联要用inverse。
0 请登录后投票
   发表时间:2004-02-20  
我试过,好像没什么问题嘛

尽量别用assigned,还有双向关联要用inverse。
代码用code格式化一下。
0 请登录后投票
   发表时间:2004-02-21  
偶找到错了,是配置文件写错了,偶用的是oracle8的数据库,而我的hibernate.property文件中我写的是:
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect

换成:hibernate.dialect net.sf.hibernate.dialect.OracleDialect
就可以跑通了....谢谢yehs220和yangstarfly
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics