-->
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: SQL Server query parameter type is mapped to NVARCHAR
PostPosted: Mon May 16, 2016 4:39 pm 
Newbie

Joined: Mon May 16, 2016 4:36 pm
Posts: 1
We have a Hibernate (Java) query that looks in a SQL Server table called MARK for a database row, searching by a (primary) key value SYMBOL which is VARCHAR.

What’s happening is that the parameter type in the incoming query is mapped to NVARCHAR, even though the column is VARCHAR.

E.g.:
Code:
(@P0 nvarchar(4000))SELECT…


This forces the lookup to full scan the index, instead of seeking out the key. When we tried to change the datatype directly, or even repoint this query to an indexed view that applied the type conversion, we got an error such as:

org.hibernate.HibernateException: Wrong column type in <database>.dbo.MARK for column SYMBOL. Found: nvarchar, expected: varchar(255)

The question is, how can we guide Hibernate to map string values to VARCHAR instead of NVARCHAR by default?


Top
 Profile  
 
 Post subject: Re: Parameter type in the incoming query is mapped to NVARCHAR,
PostPosted: Tue May 17, 2016 2:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There are two options to you could try:

1. The SQL Server JDBC driver defines the sendStringParametersAsUnicode connection property which is true by default. You could set it to false, and then all VARCHARs are going to be sent in ASCII.

2. You could hint Hibernate about the parameter type:

Code:
org.hibernate.Query q = ...;
q.setParameter("symbol", symbol, StringType.INSTANCE);


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.