-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why does flush()-ing twice in a row lead to an error?
PostPosted: Tue Sep 30, 2003 4:33 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 4:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Umm I reckon you have a broken get/set pair or something like that. Something that causes the first flush to actually change the persistent object by side effect.


Top
 Profile  
 
 Post subject: Get/Sets should never have side effects, correct?
PostPosted: Tue Sep 30, 2003 4:45 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
Thanks.


Top
 Profile  
 
 Post subject: Follow up
PostPosted: Tue Sep 30, 2003 5:14 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
So, to make sure I understand:

1. As a general rule, getters and setters in Hibernate should NEVER have side effects.

2. As a general rule, doing two Flush() calls in a row should indeed be a no-op.

Correct?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 6:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
As a general rule, getters and setters in Hibernate should NEVER have side effects.


Ummmm. I wouldn't go so far as to say "never" .... but in general, and speaking approximately:

foo.setBar(bar);
assertTrue( foo.getBar().equals(bar) );

should hold.

Quote:
As a general rule, doing two Flush() calls in a row should indeed be a no-op.


Yes, of course.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 9:45 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 4:16 pm
Posts: 33
gavin wrote:
foo.setBar(bar);
assertTrue( foo.getBar().equals(bar) );

should hold.


But also would you agree that
for (int i=0; i<100; i++) foo.setBar(bar);

should cause no ill effects?


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

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.