package many;
public class Publication {
private String id;
private String bookName = null;
private String stateDate = null;
private Author author = null;
/**
 * Method setId
 *
 *
 * @param id
 *
 */
public void setId(String id) {
// TODO: Add your code here
this.id = id;
} /**
 * Method getId
 *
 *
 * @return
 *
 */
public String getId() {
// TODO: Add your code here
return this.id;
} /**
 * Method getBookName
 *
 *
 * @return
 *
 */
public String getBookName() {
// TODO: Add your code here
return this.bookName;
} /**
 * Method setBookName
 *
 *
 * @param bookName
 *
 */
public void setBookName(String bookName) {
// TODO: Add your code here
this.bookName = bookName;
} /**
 * Method setDate
 *
 *
 * @param date
 *
 */
public void setStateDate(String stateDate) {
// TODO: Add your code here
this.stateDate = stateDate;
} /**
 * Method getDate
 *
 *
 * @return
 *
 */
public String getStateDate() {
// TODO: Add your code here
return this.stateDate;
}
public void setAuthor( Author author )
{
this.author = author;
}
public Author getAuthor()
{
return this.author;
}
}

解决方案 »

  1.   

    package many;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.MappingException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.Transaction;
    import net.sf.hibernate.cfg.Configuration;
    import net.sf.hibernate.tool.hbm2ddl.SchemaExport;public class Example{
        
        private static SessionFactory _sessions = null;
        private static Properties pops = new Properties();
        
        static{
            try {
                
                Configuration cfg = new Configuration();
                
                cfg.addClass(Author.class);
                cfg.addClass(Publication.class);
                SchemaExport dbExport = new SchemaExport(cfg);
                dbExport.create(true, true);
                _sessions = cfg.buildSessionFactory();
                
            } catch (MappingException e) {
               e.printStackTrace();
            } catch (HibernateException e) {
               e.printStackTrace();
            }
        
        }
        
        public static void main(String[] args) throws HibernateException {
            
            
            Publication publication1 = new Publication();
            publication1.setId("00001");
            publication1.setBookName("AAA");
            publication1.setStateDate("20031224");
            Publication publication2 = new Publication();
            publication2.setId("00002");
            publication2.setBookName("BBB");
            publication2.setStateDate("20031225");
            
            Author author = new Author(); author.setId("Z0001");
            author.setName("smallduzi");
            author.getPublications().add(publication1);
            author.getPublications().add(publication2);
            
            //publication1.setAuthor(author);
            //publication2.setAuthor(author);        Session session = _sessions.openSession();
            
            Transaction tx = null;
            try{
                tx = session.beginTransaction();
                session.save(author);
                session.flush();
                tx.commit();
               
                System.out.println("over");
            }catch(HibernateException he){
                if(tx != null) tx.rollback();
                throw he;
            }
            finally{
                session.close();
            }
            
        }
        
    }
      

  2.   

    <?xml version="1.0" encoding="GB2312"?>
    <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
    <hibernate-mapping>
        <class name="many.Author" table="author" dynamic-update="true" dynamic-insert="true"> 
            <id name="id" column="id">
    <!--uuid.hex-->
                <generator class="assigned"/>
            </id>
            <property name="name" column="Name" />
            <set name="publications" lazy="false" inverse="true" cascade="all">
              <key column="author_id"/> 
              <one-to-many class="many.Publication" />
            </set>
        </class>
    </hibernate-mapping>
      

  3.   

    <?xml version="1.0" encoding="GB2312"?>
    <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
    <hibernate-mapping>
        <class name="many.Publication" table="Publication" dynamic-update="true" dynamic-insert="true"> 
            <id name="id" column="Id">
    <!--
                <generator class="sequence">
    <param name="sequence">Reader_Id_Seq</param>
                </generator>
    -->
     <generator class="assigned"/>
            </id>
            <property name="bookName" column="BookName" />
            <property name="stateDate" column="stateDate" />
            <many-to-one name="author" cascade="all" outer-join="auto" update="true" insert="true" column="author_id" />
        </class>
    </hibernate-mapping>
      

  4.   

    输出以及错误信息如下:在这里为什么是
    update Publication set BookName=?, stateDate=?, author_id=? where Id=?而不是insert呢?问题就可能在这里。请帮忙看看。create table Publication (
       Id VARCHAR2(255) not null,
       BookName VARCHAR2(255),
       stateDate VARCHAR2(255),
       author_id VARCHAR2(255),
       primary key (Id)
    )
    create table author (
       id VARCHAR2(255) not null,
       Name VARCHAR2(255),
       primary key (id)
    )
    alter table Publication add constraint FK23254A0C57F3E04F foreign key (author_id) references author
    Hibernate: insert into author (Name, id) values (?, ?)
    Hibernate: update Publication set BookName=?, stateDate=?, author_id=? where Id=?
    Exception in thread "main" net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
            at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
            at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
            at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:611)
            at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:31)
            at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
            at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2062)
            at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
            at many.Example.main(Example.java:66)
      

  5.   

    inverse="true" 把它去了试试看!
      

  6.   

    hbm.xml id 属性加上unsaved-value="0"
      

  7.   

    1.你的Publication(child)的主键是assigned
    2.你的Author(parent)的inverse=false,cascade="all"表明由parent控制关联,并且级联save,update,delete child.
    3.save parent时,hibernate发现parent拥有的child的主键都不为空,认为children都是已持久化的po,于是调用update 的sql更新所有的child状态.
    4.你的数据库中没有存储期待被更新的po的数据,发生异常.