因为你没有设置inverse=true,所以父亲也要维持父子关系,因为它不知道哪个儿子指向自己,所以就多出update,所以影响效率,如果设置了inverse=true,就只需要儿子来维护关系,需要显示的指定son.setParent(parent).

解决方案 »

  1.   

    我的inverse=true,对不起,我没有贴配置文件
    父如下:<set name="personAttrVal"
         table="personAttrVal"
     lazy="false"
     inverse="true" 
     cascade="all"
     sort="unsorted"
     order-by="pkIDPersonAttrVal"
     >
     <key column="pkPersonID"/>
     <one-to-many class="com.madcat.resource.PersonAttrVal"/>
        </set>
    子如下:<many-to-one
        name="personMain"
    class="com.madcat.resource.PersonMain"
        cascade="all"
        outer-join="auto" 
    update="true" 
    insert="true" 
    access="property"  
    not-null="true">
    <column name="pkPersonID"/>
    </many-to-one>
      

  2.   

    错误如下:
    net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
    at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
    at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
    at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
    at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
    at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
    at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
    at com.madcat.testhibernate.testhibernate.testInsert(testhibernate.java:81)
    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 junit.textui.TestRunner.doRun(TestRunner.java:116)
    at com.intellij.rt.execution.junit2.IdeaJUnitAgent.doRun(Unknown Source)
    at junit.textui.TestRunner.start(TestRunner.java:172)
    at com.intellij.rt.execution.junit.TextTestRunner2.startRunnerWithArgs(Unknown Source)
    at com.intellij.rt.execution.junit2.JUnitStarter.prepareStreamsAndStart(Unknown Source)
    at com.intellij.rt.execution.junit2.JUnitStarter.main(Unknown Source)
    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 com.intellij.rt.execution.application.AppMain.main(Unknown Source)
    net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
    at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
    at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
    at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
    at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
    at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
    at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
    at com.madcat.testhibernate.testhibernate.testInsert(testhibernate.java:81)
    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 junit.textui.TestRunner.doRun(TestRunner.java:116)
    at com.intellij.rt.execution.junit2.IdeaJUnitAgent.doRun(Unknown Source)
    at junit.textui.TestRunner.start(TestRunner.java:172)
    at com.intellij.rt.execution.junit.TextTestRunner2.startRunnerWithArgs(Unknown Source)
    at com.intellij.rt.execution.junit2.JUnitStarter.prepareStreamsAndStart(Unknown Source)
    at com.intellij.rt.execution.junit2.JUnitStarter.main(Unknown Source)
    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 com.intellij.rt.execution.application.AppMain.main(Unknown Source)
      

  3.   

    一对多不要用table了<set name="personAttrVal"
        <!-- table="personAttrVal"--去掉这个就OK-->
     lazy="false"
     inverse="true" 
     cascade="all"
     sort="unsorted"
     order-by="pkIDPersonAttrVal"
     >
     <key column="pkPersonID"/>
     <one-to-many class="com.madcat.resource.PersonAttrVal"/>
        </set>
      

  4.   

    <!-- table="personAttrVal"--去掉这个就OK-->
      

  5.   

    不可能撒,给你一个最简单的配置文件。
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
    <class name="cn.com.stoneman.sztvdemo.ParentBean" table="parent">
    <id name="parentid" column="parentid" type="integer">
    <generator class="native"/>
    </id>
    <property name="name" column="name" type="string"/>
    <bag name="childBean" cascade="all" order-by="childid">
    <key column="parentid"/>
    <one-to-many class="cn.com.stoneman.sztvdemo.ChildBean"/>
    </bag>
    </class>
    </hibernate-mapping><hibernate-mapping>
    <class name="cn.com.stoneman.sztvdemo.ChildBean" table="child">
    <id name="childid" column="childid" type="integer">
    <generator class="native"/>
    </id>
    <property name="name" column="name" type="string"/>
    <many-to-one name="parent" column="parentid" class="cn.com.stoneman.sztvdemo.ParentBean"/>
    </class>
    </hibernate-mapping>
    你参照一下改一下吧,最好能把你关键的代码贴出来
      

  6.   

    我测试的代码如下:
     PersonAttrVal personAttrVal = new PersonAttrVal();
            personAttrVal.setPkidpersonAttrDef(1);
            personAttrVal.setAttrValue("鸟");
            personAttrVal.setStatus((short)0);        PersonMain personmain = new PersonMain();
            personmain.setAddress("aa");
            personmain.setBirthday(new Date());
            personmain.setCreateTime(new Date());
            personmain.setEmail("aaa");
            personmain.setMemo("aaa");
            personmain.setPassword("aaa");
            personmain.setPersonNameSpell("aaa");
            personmain.setPersonName("阿嗄阿");
            personmain.setPersonType((short)1);
            personmain.setPhoneNumber("123123");
            personmain.setPolity("aaa");
            personmain.setSex((short)1);
            personmain.setStatus((short)0);
    //        personmain.setUpdateTime(Timestamp.valueOf("2001-1-1"));
            personmain.setUpdateTime(new Date());
            personmain.setUserId("aaa");
            personmain.getPersonAttrVal().add(personAttrVal);        personAttrVal.setPersonMain(personmain);        try {
                Transaction tx = session.beginTransaction();
                session.saveOrUpdate(personmain);
    //            session.save(personAttrVal);
                tx.commit();
    //            session.flush();        } catch (HibernateException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
      

  7.   

    personAttrVal.setPkidpersonAttrDef(1);
    你的主键生成策略是什么,如果是自增的话,把这句话注释
    personmain.getPersonAttrVal().add(personAttrVal);//注释掉
    personAttrVal.setPersonMain(personmain);改成personmain.setPersonAttrVal(personAttrVal);