File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Does this Hibernate generated SQL mean anything to anyone? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Does this Hibernate generated SQL mean anything to anyone?" Watch "Does this Hibernate generated SQL mean anything to anyone?" New topic
Author

Does this Hibernate generated SQL mean anything to anyone?

Andles Jurgen
Ranch Hand

Joined: Mar 18, 2002
Posts: 67
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

Joined: Nov 02, 2004
Posts: 29
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

Joined: Mar 18, 2002
Posts: 67
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

Joined: Jun 19, 2001
Posts: 547
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

Joined: Jun 19, 2001
Posts: 547
oh and your SQL statements look fine to mee.
Each ? will be replaced with the actual value.

pascal
Ritchie Warsi
Greenhorn

Joined: Nov 02, 2004
Posts: 29
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

Joined: Jun 19, 2001
Posts: 547
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

Joined: Mar 18, 2002
Posts: 67
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

Joined: Jun 19, 2001
Posts: 547
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jan 07, 2002
Posts: 89
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.


Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does this Hibernate generated SQL mean anything to anyone?