Hibernate一对一映射更新,而不是插入和抛出StaleStateException

时间:2012-07-01 22:20:53

标签: java hibernate one-to-one

我正在尝试使用hibernate进行一对一映射,将一些信息插入数据库,但每次尝试都会出现此错误。

Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1217)
at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:145)
... 12 more

这是我的client.hbm.xml文件。

<hibernate-mapping>
<class name="register.Client" table="client" lazy="false">
    <id name="clientId" type="string" column="CLIENT_ID"
        length="255">
        <generator class="increment"></generator>
    </id>
    <property name="address" type="string" column="ADDRESS"
        length="255" />
    <property name="email" type="string" column="EMAIL"
        length="255" />
    <property name="username" type="string" column="USERNAME"
        unique="true">
    </property>


    <!-- Associations -->
    <!-- bi-directional one-to-one association to Login -->
    <one-to-one name="login" class="login.UsrPwd" lazy="false"
         outer-join="auto" />
</hibernate-mapping>

这是我的login.hbm.xml文件

<hibernate-mapping>

<class name="login.UsrPwd" table="login">

    <id name="username" type="string" column="USER_NAME">   </id>   
    <property name="password" column="PASSWORD" type="string"/>
    <property name="type" column="TYPE_ID" type="int"></property>
    <one-to-one name="consultantInfo" class="register.ConsultantInfo"
        cascade="all" />

 <!--        bi-directional one-to-one association to Client -->
    <one-to-one name="client" class="register.Client"  cascade="all"/>
     </class>
 </hibernate-mapping>

当我运行我的函数将信息添加到数据库时,这就是我得到的SQL。

 Hibernate: insert into login (PASSWORD, TYPE_ID, USER_NAME) values (?, ?, ?)
 Hibernate: update client set ADDRESS=?, EMAIL=?, USERNAME=? where CLIENT_ID=?

我不认为第二个应该更新,因为该行还不存在,这就是为什么它会抛出我得到的异常。我不知道为什么会这样做,任何帮助都会受到赞赏。

如果您需要更多信息,请告诉我,我可以提供

0 个答案:

没有答案