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