Call Stored Procedure Hibernate

Call Stored Procedure Hibernate

Many approaches are there to call stored procedure in Hibernate and finally you get same results with any call. In terms of performance every approaches gives same performance and choice is yours which suits your needs. Only suggestion is: if you are using Hibernate its better to go with ORM approach.

  • Create table script:

  • Insert data script:

  • Create stored procedure script:

As you see stored procedure will take one eventDate as parameter to return the data:

  • Execute script which can be use directly to call store procedure using any database utility:

 

Hibernate to call stored procedure: I will show you five different approach to call stored procedure:

  • By using Native SQL query:

  • By using NamedNativeQuery: In your java model class please add @NamedNativeQuery annotation as below:

  • Java Dao call:

  • By using pure SQL query:

  • By using scaler with types: If you are using Scaler then you don’t need to map your java model with hibernate any annotation and it should be just plain Java POJO class with getter and setter method shown as below for Test table:

  • Java Dao call:

  • By using scaler without types: Is same as above but here you don’t need to use any hard coded types as above and your Java POJO class should match variable types with database column types:

 

That’s it. For reference I am including full maven project for download complete model and Dao class below:

  • TestModelForProcedure.java:

  • TestModelWithoutColumnMapping.java:

  • StoredProcedureDaoHandler.java:

download Download Project: HibernateStoredProcedureCall

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *