-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Pass table type data to SQL Server stored procedure
PostPosted: Mon Sep 04, 2017 8:32 am 
Newbie

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

I want to pass a table or two integer arrays(any of these two options as long as it works) to a stored procedure. I have a POJO with this structure:
int id;
int order;
and I want to send a list of these kind of objects, so practically, a table with two int columns (id, order).

From what I found on the internet until now, there is a possibility using a data table type in SQL. I already created one with:
CREATE TYPE [dbo].[WebSortOrderTable] AS TABLE(
[id] [int] NOT NULL,
[web_sort_order] [int] NULL
)

The problem that I have is that I don't know how to pass the input to the stored procedure with hibernate.

The stored procedure looks like this:
CREATE procedure [dbo].[spwebSortOrderUpdate]
(@webSortOrderArray dbo.WebSortOrderTable READONLY)
AS
BEGIN
UPDATE dbo.Ch_Profiles_complex_items
SET Ch_Profiles_complex_items.web_sort_order = array.web_sort_order
FROM dbo.Ch_Profiles_complex_items INNER JOIN
@webSortOrderArray AS array ON array.id = Ch_Profiles_complex_items.row_id
END

Thank you in advance!


Top
 Profile  
 
 Post subject: Re: Pass table type data to stored procedure
PostPosted: Mon Sep 04, 2017 10:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
I'm not sure if you can do it with the Hibernate specific API, but you can always get access to the underlying JDBC Connection as follows:

Code:
session.doWork(connection -> {
    try (SQLServerCallableStatement stmt =
      (SQLServerCallableStatement) con.prepareCall("{call spwebSortOrderUpdate(?)}")) {

      SQLServerDataTable table = new SQLServerDataTable();   
      sourceDataTable.addColumnMetadata("id", java.sql.Types.INTEGER);   
      sourceDataTable.addColumnMetadata("web_sort_order", java.sql.Types.INTEGER);   

      sourceDataTable.addRow(1, 2);
      sourceDataTable.addRow(3, 4);

      stmt.setStructured(1, "dbo.WebSortOrderTable", table); 
   }
});


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.