-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to pass an array as a parameter to a stored procedure?
PostPosted: Mon Jan 08, 2007 11:34 am 
Newbie

Joined: Mon Jan 08, 2007 11:30 am
Posts: 3
Someone else asked this question but I think the question was misunderstood.

I have a stored procedure that takes an array of strings and an int as parameters. The org.hibernate.Query class does not have a setArray method, only setInt, setString etc.

If you do this invocation with straight jdbc you need to create an ArrayDescriptor and an Oracle ARRAY object and then pass that into the CallableStatement.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 2:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so write a custom type that does what you want it to and use it with Query.setParameter(name, value, Hibernate.custom(YourArrayType.class));

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 5:29 pm 
Newbie

Joined: Mon Jan 08, 2007 11:30 am
Posts: 3
Thanks for the response.

Does this work? Has this been tried before and proven to work?
I will try it but I'm concerned that hibernate won't know how to turn my custom type into an ArrayDescriptor.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 5:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
since you write the custom type you write the code that does the conversion....and yes it has been done before ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 10:55 am 
Newbie

Joined: Mon Jan 08, 2007 11:30 am
Posts: 3
You are the man!
That worked.

Now how am I supposed to make my code platform independent and work with different dialects?

Here is my nullSafeGet method which is mapping a List of objects to an Oracle type defined as TANGO.STRING_ARRAY which is a TABLE OF VARCHAR2(255)

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
if (value == null)
statement.setNull(index, SQL_TYPES[0]);
else if(value instanceof ARRAY){
statement.setArray(index, (ARRAY) value);
}
else if(value instanceof List)
{
List valueList = (List)value;
String[] stringArray = new String[valueList.size()];
valueList.toArray(stringArray);
ArrayDescriptor arrayDesc = ArrayDescriptor.createDescriptor("TANGO.STRING_ARRAY", statement.getConnection());
ARRAY array = new oracle.sql.ARRAY(arrayDesc, statement.getConnection(), stringArray);
statement.setArray(index, array);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 11:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hey it ain't me who is using db specifc functionallity ;)

_________________
Max
Don't forget to rate


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