There is no problem with oracle database query. Two Chinese characters are displayed normally. But how to solve this problem when only the first Chinese character is displayed during Hibernate query in the java background ?, Oraclehibernate
Select case national when 1 then 'Han nationality 'end from info
The national field is of the number type and has 10 lengths. If the database query result is normal, the Han nationality is displayed if it is 1, but only one "Han" word can be displayed during the java code Hibernate query, I asked my colleagues what was going on. I saw a blog on the Internet and understood what was going on;
A constant is considered to be of the CHAR type in the database. When Hibernate takes a value, it is saved as the Character type, while Character can only save one Character. Therefore, only one Character exists in the returned value. This bug already exists in Hibernate, but it has never been fixed.
This BUG provides two solutions:
1. inherit a Dialect and register the corresponding CHAR type. The Code is as follows:
public class MySQLServerDialect extends org.hibernate.dialect.SQLServerDialect { public MySQLServerDialect() { super(); //very important, mapping char(n) to String registerHibernateType(Types.CHAR, Hibernate.STRING.getName()); } }
2. set the type of the field (if you query many fields, each field must be set. fields without the set type do not exist in the result set), such as session. createSQLQuery ("select 'Total' as name from dual "). addScalar ("name", Hibernate. STRING ). list ()
3. Another solution is to set the field type in SQL. The Code is as follows. I use this method, and others are not verified:
Select cast (case national when 1 then 'Han nationality 'end as varchar2 (4) from info
This method is available in Oracle and SQL Server.