0

首先,请参阅 Hibernate 映射摘录:Oracle DB TABLE 中有可变的 ParentObjects 引用 Oracle DB VIEW 中的不可变 ChildObjects:

1)一个ParentObject里面有一组ChildObjectInView,定义为不可变,没有级联:

<class name="ParentObject" table="t_parentobject">
...
    <set name="childObjectsInView" cascade="none" lazy="true" mutable="false">
        <key column="coivId" />
        <one-to-many class="com.it.ChildObjectInView"/>
    </set>      
</class>

2) ChildObjectInView 定义为

<class name="ChildObjectInView" table="view_coiv" mutable="false" lazy="true">
...
    <many-to-one name="parentObject" column="parentObjectId" update="false" insert="false" class="com.it.ParentObject" not-null="true" outer-join="true">
    </many-to-one>

</class>

调用 com.it.TestServiceImpl。saveParentObject()导致 Oracle 错误ORA-01732:尽管在休眠映射中设置了 mutable="false" 属性,但此视图上的数据操作操作不合法。为什么会出现这个错误?

com.it.TestBean|could not delete collection: [com.it.ParentObject.childObjectsInView#398500]
org.hibernate.exception.SQLGrammarException: could not delete collection: [com.it.ParentObject.childObjectsInView#398500]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1071)
        at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:28)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at com.it.TestServiceImpl.saveParentObject(TestServiceImpl.java:418)

Caused by: java.sql.BatchUpdateException: ORA-01732: data manipulation operation not legal on this view
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10296)
        at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:216)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
        at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:34)
        at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1048)
        ... 69 more
4

1 回答 1

0

I finally found out by myself that there were cases in that a ParentObject was newly created with the set ParentObject.childObjectsInView being set to null.

Saving this newly created ParentObject Hibernate tried to run a DELETE FROM <view> WHERE id=<objectid>. This statement is like a reset of all objects inside ParentObject.childObjectsInView.

I think this default behavior of Hibernate is wrong at this place, a bug report should be opened: Hibernate has the information that the child is immutable so any manipulating SQL should be suppressed.

Workaround: Assure that an empty collection is always set to ParentObject.childObjectsInView (instead of null).

于 2013-02-13T09:16:27.170 回答