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 hibernate and PL/SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate and PL/SQL" Watch "hibernate and PL/SQL" New topic
Author

hibernate and PL/SQL

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
1. if I use hibernate, do I have to use HQL or can I still use plain SQL alnguage like "select * from Table where..". ?

2. Can I call stored procedure from Hibernate ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Hibernate gives you three choices for writing queries. There is the Criteria Object which is building queries with Java objects. HQL is like writing SQL but instead of tables and fields, you use objects and object attributes. And the third is straight SQL, you can do any combination of them, so for one use case you might find it easiest to use Criteria, another HQL and yet a third in SQL.

You can call stored procedures with a couple caveats.

1. There can only be one out parameter from your SP and it must be the first parameter.
2. The out parameter can only be a ref cursor. It cannot be a custom data type et al.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Indeed, you can send native SQL code calls to Hibernate. It's not a best practice, but the methods on the Hibernate Session definitely make it possible.

-Cameron McKenzie
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Just thought I'd throw in an example of hitting the database with standard SQL and a native Hibernate query for your pleasure:

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Thank you guys so much !!
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
I just want to use hibernate to call a stored procedure that does not return anything but does some database insert and update. Is that feasible ? what's the standard syntax for this ?

Thank you
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Originally posted by Raj Ohadi:
I just want to use hibernate to call a stored procedure that does not return anything but does some database insert and update. Is that feasible ? what's the standard syntax for this ?

Thank you


Yes, it is pretty easy to set your mappings to call insert and update stored procedures instead of Hibernate generating the insert and update statements. For each class mapping you can include a <insert-sql> or <update-sql>. I am pretty sure those are the tags, I just don't memorize that stuff. But basically in each entity that you map you can include tags to tell Hibernate to call SPs instead of creating the SQL.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate and PL/SQL