I am using Hibernate with HSQL to build a stand-alone client application. I wrote some test code which is behaving in a way that doesn't make sense to me:
Code:
{
Session session;
ChannelBuilder builder = null;
Transaction tx = null;
int chan_id;
try
{
session = handler.getSession(conn);
tx = session.beginTransaction();
builder = new ChannelBuilder(session);
Channel channel = (Channel) builder.createChannel(chanName);
chan_id = channel.getIntId();
channel.setDescription("Test Channel: " + chanName + " ID = " + chan_id);
+ chan_id);
// session.saveOrUpdate(channel);
for (int i = 0; i < count; i++)
{
ItemIF item1 =
builder.createItem(
channel,
"Item " + i + " for " + chanName,
"A wonderful description!",
new URL("http://www.sf.net/"));
session.saveOrUpdate(item1);
session.saveOrUpdate(channel);
}
// session.flush();
tx.commit();
session.close();
Notice the commented out session.flush() near the bottom. With that call commented out, this piece of code appears to work correctly. When I look at the tx.commit() code I notice that one of the first things it does is a flush. So I would think that the explicit flush that I added would be redundant and a no-op. However if I include the explicit flush I get this error message:
Code:
Hibernate: insert into CHANNELS (TITLE, DESCRIPTION, LOCATION, SITE, CREATOR, PUBLISHER, LANGUAGE, FORMAT, IMAGE_ID, TEXTINPUT_ID, COPYRIGHT, RATING, GENERATOR, DOCS, TTL, LAST_UPDATED, LAST_BUILD_DATE, PUB_DATE, UPDATE_PERIOD, UPDATE_FREQUENCY, UPDATE_BASE, CHANNEL_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: call IDENTITY()
Hibernate: insert into ITEMS (CHANNEL_ID, TITLE, DESCRIPTION, LINK, CREATOR, SUBJECT, DATE, FOUND, GUID, COMMENTS, SOURCE, ENCLOSURE, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: call IDENTITY()
Hibernate: insert into ITEMS (CHANNEL_ID, TITLE, DESCRIPTION, LINK, CREATOR, SUBJECT, DATE, FOUND, GUID, COMMENTS, SOURCE, ENCLOSURE, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: call IDENTITY()
Hibernate: insert into ITEMS (CHANNEL_ID, TITLE, DESCRIPTION, LINK, CREATOR, SUBJECT, DATE, FOUND, GUID, COMMENTS, SOURCE, ENCLOSURE, ITEM_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: call IDENTITY()
Hibernate: update CHANNELS set TITLE=?, DESCRIPTION=?, LOCATION=?, SITE=?, CREATOR=?, PUBLISHER=?, LANGUAGE=?, FORMAT=?, IMAGE_ID=?, TEXTINPUT_ID=?, COPYRIGHT=?, RATING=?, GENERATOR=?, DOCS=?, TTL=?, LAST_UPDATED=?, LAST_BUILD_DATE=?, PUB_DATE=?, UPDATE_PERIOD=?, UPDATE_FREQUENCY=?, UPDATE_BASE=? where CHANNEL_ID=?
Hibernate: update ITEMS set CHANNEL_ID=? where ITEM_ID=?
Hibernate: update ITEMS set CHANNEL_ID=null where CHANNEL_ID=?
(util.JDBCExceptionReporter 38 ) SQL Error: -10, SQLState: 23000
(util.JDBCExceptionReporter 46 ) Try to insert null into a non-nullable column in statement [update ITEMS set CHANNEL_ID=null where CHANNEL_ID=20]
(util.JDBCExceptionReporter 37 ) Could not synchronize database state with session
Clearly the extra flush is doing something, and so clearly my understanding of what flush does is confused.
(I've done my best to read all the doc, faqs, and forum postings)
Can someone clarify?
Thanks