Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: CALL a stored procedure that does INSERT
PostPosted: Tue Aug 22, 2017 1:32 am 

Joined: Tue Aug 22, 2017 1:17 am
Posts: 2

I am new to Hibernate and I have a problem.

I am trying to CALL a stored procedure from a database. The stored procedure it's supposed to INSERT or UPDATE in 3 different tables. The problem is that the procedure isn't even executed at all.
I am trying to CALL it with createNativeQuery({CALL procedureName (:param1, param2 , ...)}), so the syntax is fine.

My doubts in this are the following:
1)I already used hibernate to call stored procedure, but the stored procedures that I called until now returned a table and I mapped them to a POJO. This time it isn't the same, since it only does some inserts.
2)When I usually executed a query with hibernate I usually first created a bean for the entity manager in the persistence.xml and api context.xml mapping the bean to a model class(the POJO) and when I called the query I did it with entityManager.createNativeQuery({CALL blabla...});. This time, since the query doesn't return me a table and it only does some inserts I thought that it does't matter what entityManager I use so I used one that was mapped to a random model class...Am I right in this statement?
3)I am following the process with SQL Server Profiler and I see that the procedure isn't executed at all when I call it
4)The error I get when I call it is: " javax.persistence.TransactionRequiredException: Executing an update/delete query" which I know that is supossed to appear when I do an insert or and update or a delete and that I should annotate the method with @Transactional...But this time I am trying only to call a stored procedure that does it, I don't want it to map the data to a POJO and persist the changes back to the database

Can you please help me with this problem?

 Post subject: Re: CALL a stored procedure that does INSERT
PostPosted: Wed Aug 23, 2017 4:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1386
Because you are using SQL Server, you need to check this article about how to use call stored procedures using Hibernate.

1) You should probably return the upateCount of the rows being changed.
2) You don't need entity mappings for calling a stored procedure.
3) Probably due to a call syntax issue which you can fix by following the this article.
4) You need a read-write transaction if you plan on modifying data.

If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.