David
David

Reputation: 11

Hibernate Queries v Database functions

I am managing the development of a Spring Web App which uses a PostgreSQL database.

I am doing the db work and supplying scripts (i.e authenticate_user) for use by the Java developer who is using Hibernate. I have limited exposure to Java and Hibernate.

I understand the basic concepts of hibernate but certainly not the full set of features it has to offer. I want to have visibility and control of the data layer but want the best solution so will go with whatever is best.

My initial idea was to store all query/insert/update scripts written as a function or procedure and allow the Java developer to simply call them and use the result/ result set rather than constructing the hibernate table references and queries within the Java classes/XML files. Our Java developer is saying that he would prefer to use hibernate for everything and is converting my functions/procedures.

Our sql statements so far are quite simple. What happens when they get to the level of complexity (or higher) as shown in the following post?

How to construct advanced Hibernate Query with OR and summarize functions

If a function/procedure is written on the database and being being called by hibernate, can the Java developer simply call them (i.e call myFunction(param) or call myProc(param)) and use the result set in exactly the same way as if he constructed the query in hibernate? For example; I have a select from multiple tables with GROUP and HAVING clauses which returns 1000 records. If I place that select into a procedure, can hibernate developer simply call the procedure and use the records in the same manner as if the select was created in hibernate only?

Many thanks, David

Upvotes: 1

Views: 511

Answers (2)

Mohammadreza Khatami
Mohammadreza Khatami

Reputation: 1332

Hibernate is an ORM which lets you some advantages. for example with hql you don't need to use native sql with different queries on different databases (postegres,mysql,etc..) . so you don't need change your queries and other things like models etc on different databases.

Upvotes: 0

Sudarshan_SMD
Sudarshan_SMD

Reputation: 2587

Hibernate is framework is used for mapping object orient domain model to traditional relational database. When working with hibernate, it becomes much easier to do operations like insert, update, delete,etc. Developer has to perform simple operation like foo.save(), foo.update() and hibernate converts it internally to sql query and executes it on database. It makes tasks much simple. On top of that, your code won't change a bit(except for configuration), if you happen to change database used.

So, best approach will be:

  • Use hibernate instead of calling procedures all the time.
  • If you want to use procedures for some complicated tasks, you can call procedures using hibernate. There are multiple ways in hibernate through which you call procedure. So, no problem here.

Select Queries: No need to use procedures for calling select. Hibernate has very simple way of firing select and it return List of objects! No need to map your result to objects. eg:

Query query = session.createQuery("from table where columnName = 'value' ");
List list = query.list();

return list of objects. No need to explicitly map resultSet to objects.

Writing all advantages of ORM over here is not possible, please read on advantages that ORM tools offer and then decide on your strategy.

Upvotes: 1

Related Questions