简体   繁体   中英

Hibernate native sql query

Perfectly fine ms-sql statement

SELECT distinct ProductLineCode ,
(SELECT CAST(ItemName + ', ' AS VARCHAR(MAX)) FROM Product spt 
where spt.ProductLineCode = pt.ProductLineCode FOR XML PATH ('')) 
as ItemNames FROM Product pt where ProductLineCode is not null

cause error when executed as native query in hibernate.

org.hibernate.MappingException: No Dialect mapping for JDBC type: -16

I guess that JDBC type: -16 is boolean but that says me nothing. Product table has xml mapping and works well for months. Could you please guide me what to try next?

You might as advised in comments be able to get something similar to work by changing the types in the sql.

But if that doesn't bring success, you might also be able to tell hibernate how to deal with this type by changing the Dialect class used by your application, possibly even extending the one currently used and adding a registration for the missing type.

It appears that your missing type mapping is for the type java.sql.Types.LONGNVARCHAR (by code grep, the value is -16), so a Dialect extension with a call something like

registerColumnType( Types.LONGNVARCHAR, "text" );

in the constructor may convince hibernate to treat this field as text.

If you do this you'll have to change the configuration to use your dialect by modifying the line

<property name="dialect">org.hibernate.dialect.SQLServer2012Dialect</property>

to use your own dialect class. (Note: the class in there is just a guess at what you might have there now.)

It's also possible that you just have hibernate using the wrong dialect for your DB, in which case just changing the configuration to the appropriate one would be better.

It's rarely necessary to use a custom Dialect , but this may be one of those times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM