Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does this Hibernate generated SQL mean anything to anyone?

 
Andles Jurgen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generated sql:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into PERSON (name, address, id) values (?, ?, ?)

Everything seems to be working - no errors at all. But my class is not being saved to the database! I get the session, do the save, the flush etc - but no action in the database itself.

Should I not be seeing the actual SQL and not the ? ? ? business? My class is folowing all the right rules (private members, no arg constructor etc

CODE:

Person person1 = new Person();
person1.setName("Basil Fawlty");
person1.setAddress("Fawlty Towers, UK");


try {
Session session = HibernateUtil.currentSession();
session.save(person1);
session.flush();
HibernateUtil.closeSession();

} catch (Exception ex) {
System.out.println("Problem with servlet code: " + ex.getMessage());
}


help appreciated.
 
Ritchie Warsi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your HibernateUtil look like? I had this problem once begonce i didn't do a commit for the transaction.
 
Andles Jurgen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That seemed to be it. Not sure why I have to be in a transaction to get it to work, the docs and all examples I have seen say nothing about this as being a requirement.

Probarbly because Oracle auto-comitt is switched off - does that make sense?
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

have you tried the recommended "pattern"



make sure transactions are commited and session is closed.

why do you call flush() yourself ?

pascal
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh and your SQL statements look fine to mee.
Each ? will be replaced with the actual value.

pascal
 
Ritchie Warsi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So It's working now?
Maybe a bit offtopic, but can someone post their good working HibernateUtil or SessionFactory here? I'm having trouble with mine (see other topci of mine).
Pascal, the code you posted, is that a custom class or in the HUtil or SessFactory or something?
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie: This is stolen from the hibernate docs :-) : chapter 9.8
You can find the HibernateUtil here
(or probably in the demo app Caveat Emptor)

pascal
[ December 23, 2004: Message edited by: pascal betz ]
 
Andles Jurgen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, all working now. I did not know you needed to start a transaction - used to the auto-commit being switched off.

I was using flush() because the "Professional Hibernate" book told me to.

Had I been using MySql I do not think I would have had this issue - all the examples I have read and seen use Hibernate - but Oracle had auto commit switched off and that kinda threw me.

When I saw the ? in the hibernate generated SQL I thought only half the job was being done and that I had done something wrong in my mapping file(because I didn't see where the 'PreparedStatement' class substitutions were being done.) By the way, why do I not see these setXXX() calls? Because it's not relavant, not efficient???

Thanks for the help guys.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no need to call flush() yourself (session will be flushed when TX is commited). from the hibernate API:

Force the Session to flush. Must be called at the end of a unit of work, before commiting the transaction and closing the session (Transaction.commit() calls this method). Flushing is the process of synchronising the underlying persistent store with persistable state held in memory.


and here is thread from hibernate forum talking about autocommit



pascal
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andles Jurgen:
I was using flush() because the "Professional Hibernate" book told me to.
In my DAO framework I call flush() for update and delete because I want to catch exceptions caused by duplicate rows and foreign key violations immediately.
By the way, why do I not see these setXXX() calls? Because it's not relavant, not efficient???
My guess is the latter. If you want to see the nitty-gritty, use the free P6Spy JDBC Driver Wrapper; it logs both the full queries with parameter values and the result sets returned.
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andles Jurgen:


When I saw the ? in the hibernate generated SQL I thought only half the job was being done and that I had done something wrong in my mapping file(because I didn't see where the 'PreparedStatement' class substitutions were being done.) By the way, why do I not see these setXXX() calls? Because it's not relavant, not efficient???

Thanks for the help guys.


In your log4j.properties you should see an entry like this:

### log JDBC bind parameters ###
#log4j.logger.net.sf.hibernate.type=info

Simply uncomment the line and set the value to 'debug' like so:

### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type=debug

When you do this, you'll be able to see the bind parameter values that hibernate will use to complete the SQL that it has written. I'm guessing that Hibernate executes the SQL (in my mind, at least) using PreparedStatements in JDBC. So, it binds the values retrieved from your objects to the '?' place holders much like you would if you were using JDBC.

Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic