-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: puzzle with session.saveOrUpdate
PostPosted: Tue Mar 29, 2005 3:23 am 
Regular
Regular

Joined: Sat Apr 10, 2004 8:39 pm
Posts: 84
puzzle with session.saveOrUpdate

I have two DAO class , they implement a method named saveOrUpdate.
It's codes like:

Code:
   public Serializable saveOrUpdate(Object obj) throws DataAccessException {
      Session session = null;
      try {
         session = sessionFactory.openSession();
         session.saveOrUpdateCopy(obj);
         session.flush();
         return obj;
      } catch (HibernateException he) {
         throw SessionFactoryUtils.convertHibernateAccessException(he);
      } finally {
         SessionFactoryUtils
               .closeSessionIfNecessary(session, sessionFactory);
      }
   }


problem 1:


Code:
Folder folder = folderDao.loadByPrimaryKey(0);

for (int i=0; i<array.length; i++) {

      Topic topic = new Topic();
[color=red]      topic.setFolder(folder)[/color]
.....................

     if (topic.getCreateTime().before(folder.getMinTime()))  {
         folder.setMinTime(topic.getCreateTime);
    }

[color=red]    topicDao.saveOrUpdate(topic);
    folderDao.saveOrUpdate(folder);    [/color]
}



I found after folderDao.saveOrUpdate(folder) excuted ,
the saved topic objecs's field of folder be set to null.

what's the problem?

if I use this codes:

Code:
topicDao.(topic);
    folderDao.update(folder);   


It's OK. what's the problem?

=========================================


problem 2:

I 've two table it's sql is

Code:
create table xtype (
   symbol varchar(16) not null,
   title varchar(30) default 'no title' not null,
   note varchar(256),

   primary key(symbol)
);

create table pigeonhole_rule (
   xtype_symbol varchar(16) not null,
   pigeonhole_type_symbol varchar(16),

   primary key(xtype_symbol),
   foreign key(xtype_symbol) references xtype(symbol),
   foreign key(pigeonhole_type_symbol) references         pigeonhole_type(symbol)
);



when I save or update xtype object, it try to update pigeonhole_rule.
it try to set the foreigh key in pigeonhole_rule to null, but the foreign key is also primary key. so it's cast exception.

How to ?


I 've set cascade="none" in Xtype.hbm.xml

Code:
    <set name="setOfPigeonholeRule" cascade="none">
      <key>
        <column name="XTYPE_SYMBOL" length="16" not-null="false"
        unique="true"  />
      </key>

      <one-to-many
      class="demos.orm.PigeonholeRule" />
    </set>

    <one-to-one
        name="pigeonholeRule"
        class="demos.orm.PigeonholeRule"
        outer-join="auto"
        cascade="none"
    />


and set cascade="none" in PigeonholeRule.hbm.xml

Code:
    <!-- bi-directional one-to-one association to Xtype -->

    <one-to-one
        name="xtype"
        class="demos.orm.Xtype"
        outer-join="auto"
        constrained="true"
        cascade="none"
    />

  </class>



Hibernate: update XTYPE set TITLE=?, NOTE=? where SYMBOL=?
Hibernate: update PIGEONHOLE_RULE set XTYPE_SYMBOL=null where XTYPE_SYMBOL=?[WARN ] [2005-03-29 15:15:53,890] net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null
[ERROR] [2005-03-29 15:15:53,890] net.sf.hibernate.util.JDBCExceptionReporter - failed batch
[WARN ] [2005-03-29 15:15:53,890] net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null
[ERROR] [2005-03-29 15:15:53,890] net.sf.hibernate.util.JDBCExceptionReporter - failed batch
[ERROR] [2005-03-29 15:15:53,890] net.sf.hibernate.impl.SessionImpl - Could not synchronize database state with session

_________________
I am the creatxr of world.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.