1

I have a sql query which works fine on sql server query window and returns results

sql query:

select * from table1 where title = N'تست'

if I put that 'N' in my query in sql, the query gets results and if I don't put 'N' no results will be returned.

the question is how can I have this query in jpql format (with that 'N')?

thanks for replying

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Lithium
  • 589
  • 2
  • 11
  • 24
  • Does the jpql query give right results it if you give the query as "from where title = :title" ? and use setParameter(title, "تست") (without N character)? – Andy Dufresne Dec 28 '14 at 11:55
  • I don't get any results when I use this jpql: select a from a where a.title = 'تست' – Lithium Dec 28 '14 at 11:57
  • Try the solutions of [this SO question](http://stackoverflow.com/questions/5237280/getting-hibernate-and-sql-server-to-play-nice-with-varchar-and-nvarchar) – Vlad Mihalcea Dec 28 '14 at 14:19
  • @VladMihalcea - That SO thread talks mainly about a performance issue. This issue seems to be about invalid results assuming the title column is a nvarchar column (and not varchar). Did I understand it incorrectly? – Andy Dufresne Dec 28 '14 at 15:24
  • That view solution sounds very interesting. Also a Hibernate UserType might be handy. – Vlad Mihalcea Dec 28 '14 at 15:36
  • @Vlad: Andy is right, it doesn't solve my problem. the column title type is nvarchar, if I change it to varchar, the problem is solved. but I don't have permission altering tables. – Lithium Dec 29 '14 at 06:47

1 Answers1

1

Try setting this Hibernate property:

hibernate.connection.defaultNChar=true

If you use an external DataSource (external connection pool like HikariCP) then you want to set the following property in the JDBC connection URL:

   sendStringParametersAsUnicode=true
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I've set this setting, and didn't work out for me. for now I've solved this by changing the collation of database to PERSIAN because the sql driver of jdbc had some problems. thanks anyway – Lithium Dec 30 '14 at 07:43
  • In most projects I worked on, we had our DB set to UTF8 and also the web server encoding was UTF8 as well. – Vlad Mihalcea Dec 30 '14 at 08:21