Hi ,
In hql queries in where clause there is a problem, it converts Turkish characters to"?" . I modified hibernate auction sample and got same problem. I can insert,update beans with Turkish chars without problem (configured in hibernate.properties using characterEncoding param). Only problem occurs in where clause of hql queries . I wrote details below.
hibernate version:3.05
mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<class name="User" table="AuctionUser" lazy="true">
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="native"/>
</id>
<natural-id mutable="true">
<property name="userName"
length="10"/>
</natural-id>
<property name="password"
not-null="true"
length="15"
column="`password`"/>
<property name="email"/>
<component name="name">
<property name="firstName"
length="50"
not-null="true"/>
<property name="initial"
column="`initial`"/>
<property name="lastName"
length="50"
not-null="true"/>
</component>
</class>
</hibernate-mapping>
codeCode:
public class Main {
private SessionFactory factory;
/**
* Demonstrates HQL projection/aggregation
*/
public void searchByUserName(String username) throws Exception {
Session s = factory.openSession();
Transaction tx=null;
try {
tx = s.beginTransaction();
List user = s.createQuery(
" select user.userName, user.email, user.password "
+ " from User as user "
+ " where user.userName = '" + username + "'"
)
.setMaxResults(100)
.list();
System.out.println("for username " + username + " matching results:" + user.size());
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
s.close();
}
}
public void createTestUsers() throws Exception {
System.out.println("Setting up some test data");
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
User seller = new User();
seller.setUserName("Kesinleş");
seller.setName( new Name("ıİÇ窺ĞğÜü", new Character('T'), "Öö testing") );
seller.setEmail("someone@here.org");
seller.setPassword("******");
s.save(seller);
}
static User mainSeller;
public static void main(String[] args) throws Exception {
final Main test = new Main();
Configuration cfg = new Configuration()
.addClass(User.class)
.setProperty(Environment.HBM2DDL_AUTO, "create");
cfg.setProperty("hibernate.show_sql", "true");
test.factory = cfg.buildSessionFactory();
test.createTestUsers();
test.searchByUserName("Kesinleş");
test.searchByUserName("8859-1");
test.factory.close();
}
}
DB : postgresql 8.0 and mysql 4.1Debug level Hibernate log excerpt:Code:
Debug 10.285 false org.hibernate.impl.SessionImpl opened session at timestamp: 4594529587707904
Debug 10.245 false org.hibernate.impl.SessionImpl after transaction completion
Debug 10.205 false org.hibernate.jdbc.JDBCContext after transaction completion
Debug 10.165 false org.hibernate.connection.DriverManagerConnectionProvider returning connection to pool, pool size: 1
Debug 10.135 false org.hibernate.jdbc.ConnectionManager closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Debug 10.095 false org.hibernate.impl.SessionImpl closing session
Debug 10.055 false org.hibernate.impl.SessionImpl after transaction completion
Debug 10.014 false org.hibernate.jdbc.JDBCContext after transaction completion
Debug 9.974 false org.hibernate.transaction.JDBCTransaction committed JDBC Connection
Debug 9.944 false org.hibernate.impl.SessionImpl before transaction completion
Debug 9.894 false org.hibernate.jdbc.JDBCContext before transaction completion
Debug 9.854 false org.hibernate.impl.SessionImpl automatically flushing session
Debug 9.814 false org.hibernate.transaction.JDBCTransaction commit
Debug 9.764 false org.hibernate.engine.PersistenceContext initializing non-lazy collections
Debug 9.724 false org.hibernate.jdbc.AbstractBatcher closing statement
Debug 9.684 false org.hibernate.jdbc.AbstractBatcher about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Debug 9.644 false org.hibernate.jdbc.AbstractBatcher about to close ResultSet (open ResultSets: 1, globally: 1)
Debug 9.594 false org.hibernate.loader.Loader done processing result set (0 rows)
Debug 9.544 false org.hibernate.loader.Loader processing result set
Debug 9.504 false org.hibernate.jdbc.AbstractBatcher about to open ResultSet (open ResultSets: 0, globally: 0)
Debug 9.464 false org.hibernate.jdbc.AbstractBatcher preparing statement
Debug 9.424 false org.hibernate.SQL select user0_.userName as col_0_0_, user0_.email as col_1_0_, user0_."password" as col_2_0_ from h3tr.AuctionUser user0_ where user0_.userName='Kesinle?' limit ?
Debug 9.384 false org.hibernate.jdbc.AbstractBatcher about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Debug 9.333 false org.hibernate.hql.ast.ErrorCounter throwQueryException() : no errors
Debug 9.303 false org.hibernate.hql.ast.QueryTranslatorImpl SQL: select user0_.userName as col_0_0_, user0_.email as col_1_0_, user0_."password" as col_2_0_ from h3tr.AuctionUser user0_ where user0_.userName='Kesinle?'
Debug 9.263 false org.hibernate.hql.ast.QueryTranslatorImpl HQL: select user.userName, user.email, user.password from org.hibernate.auction.User as user where user.userName = 'Kesinleş'
Debug 9.203 false org.hibernate.hql.ast.ErrorCounter throwQueryException() : no errors
Debug 9.163 false org.hibernate.hql.ast.AST --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (h3tr.AuctionUser)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DOT] DotNode: 'user0_.userName' {propertyName=userName,dereferenceType=4,propertyPath=userName,path=user.userName,tableAlias=user0_,className=org.hibernate.auction.User,classAlias=user}
| | +-[ALIAS_REF] IdentNode: 'user0_.id' {alias=user, className=org.hibernate.auction.User, tableAlias=user0_}
| | \-[IDENT] IdentNode: 'userName' {originalText=userName}
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_0_'
| +-[DOT] DotNode: 'user0_.email' {propertyName=email,dereferenceType=4,propertyPath=email,path=user.email,tableAlias=user0_,className=org.hibernate.auction.User,classAlias=user}
| | +-[ALIAS_REF] IdentNode: 'user0_.id' {alias=user, className=org.hibernate.auction.User, tableAlias=user0_}
| | \-[IDENT] IdentNode: 'email' {originalText=email}
| +-[SELECT_COLUMNS] SqlNode: ' as col_1_0_'
| +-[DOT] DotNode: 'user0_."password"' {propertyName=password,dereferenceType=4,propertyPath=password,path=user.password,tableAlias=user0_,className=org.hibernate.auction.User,classAlias=user}
| | +-[ALIAS_REF] IdentNode: 'user0_.id' {alias=user, className=org.hibernate.auction.User, tableAlias=user0_}
| | \-[IDENT] IdentNode: 'password' {originalText=password}
| \-[SELECT_COLUMNS] SqlNode: ' as col_2_0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[user], fromElementByTableAlias=[user0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'h3tr.AuctionUser user0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=user,role=null,tableName=h3tr.AuctionUser,tableAlias=user0_,colums={,className=org.hibernate.auction.User}}
\-[WHERE] SqlNode: 'where'
\-[EQ] SqlNode: '='
+-[DOT] DotNode: 'user0_.userName' {propertyName=userName,dereferenceType=4,propertyPath=userName,path=user.userName,tableAlias=user0_,className=org.hibernate.auction.User,classAlias=user}
| +-[ALIAS_REF] IdentNode: 'user0_.id' {alias=user, className=org.hibernate.auction.User, tableAlias=user0_}
| \-[IDENT] IdentNode: 'userName' {originalText=userName}
\-[QUOTED_STRING] LiteralNode: ''Kesinle?''
Debug 9.123 false org.hibernate.hql.antlr.HqlSqlBaseWalker query() >> end, level = 1
Debug 9.083 false org.hibernate.hql.ast.JoinProcessor Using FROM fragment [h3tr.AuctionUser user0_]
Debug 9.043 false org.hibernate.hql.ast.HqlSqlWalker processQuery() : ( SELECT ( {select clause} ( user0_.userName user0_.id userName ) ( user0_.email user0_.id email ) ( user0_."password" user0_.id password ) ) ( FromClause{level=1} h3tr.AuctionUser user0_ ) ( where ( = ( user0_.userName user0_.id userName ) 'Kesinle?' ) ) )
Debug 9.043 false org.hibernate.hql.antlr.HqlSqlBaseWalker query() : finishing up , level = 1
Debug 9.033 false org.hibernate.hql.ast.FromReferenceNode Resolved : user.userName -> user0_.userName
Debug 8.993 false org.hibernate.hql.ast.DotNode getDataType() : userName -> org.hibernate.type.StringType@5a2eaa
Debug 8.953 false org.hibernate.hql.ast.FromReferenceNode Resolved : user -> user0_.id
Debug 8.923 false org.hibernate.hql.ast.FromReferenceNode Resolved : user.password -> user0_."password"
Debug 8.923 false org.hibernate.hql.ast.DotNode getDataType() : password -> org.hibernate.type.StringType@5a2eaa
Debug 8.923 false org.hibernate.hql.ast.FromReferenceNode Resolved : user -> user0_.id
Debug 8.913 false org.hibernate.hql.ast.FromReferenceNode Resolved : user.email -> user0_.email
Debug 8.913 false org.hibernate.hql.ast.DotNode getDataType() : email -> org.hibernate.type.StringType@5a2eaa
Debug 8.913 false org.hibernate.hql.ast.FromReferenceNode Resolved : user -> user0_.id
Debug 8.913 false org.hibernate.hql.ast.FromReferenceNode Resolved : user.userName -> user0_.userName
Debug 8.913 false org.hibernate.hql.ast.DotNode getDataType() : userName -> org.hibernate.type.StringType@5a2eaa
Debug 8.893 false org.hibernate.hql.ast.FromReferenceNode Resolved : user -> user0_.id
Debug 8.853 false org.hibernate.hql.ast.FromElement FromClause{level=1} : org.hibernate.auction.User (user) -> user0_
Debug 8.783 false org.hibernate.hql.antlr.HqlSqlBaseWalker query() << begin, level = 1
Debug 8.693 false org.hibernate.hql.ast.ErrorCounter throwQueryException() : no errors
Debug 8.693 false org.hibernate.hql.ast.AST --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'from'
| | \-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[IDENT] 'org'
| | | | | \-[IDENT] 'hibernate'
| | | | \-[IDENT] 'auction'
| | | \-[IDENT] 'User'
| | \-[ALIAS] 'user'
| \-[SELECT] 'select'
| +-[DOT] '.'
| | +-[IDENT] 'user'
| | \-[IDENT] 'userName'
| +-[DOT] '.'
| | +-[IDENT] 'user'
| | \-[IDENT] 'email'
| \-[DOT] '.'
| +-[IDENT] 'user'
| \-[IDENT] 'password'
\-[WHERE] 'where'
\-[EQ] '='
+-[DOT] '.'
| +-[IDENT] 'user'
| \-[IDENT] 'userName'
\-[QUOTED_STRING] ''Kesinle?''
Debug 8.643 false org.hibernate.hql.ast.QueryTranslatorImpl parse() - HQL: select user.userName, user.email, user.password from org.hibernate.auction.User as user where user.userName = 'Kesinleş'
Debug 8.552 false org.hibernate.engine.QueryParameters named parameters: {}
Debug 8.532 false org.hibernate.impl.SessionImpl find: select user.userName, user.email, user.password from User as user where user.userName = 'Kesinleş'
Debug 8.512 false org.hibernate.jdbc.ConnectionManager running Session.finalize()
Debug 8.472 false org.hibernate.transaction.JDBCTransaction current autocommit status: false
Debug 8.472 false org.hibernate.connection.DriverManagerConnectionProvider using pooled JDBC connection, pool size: 0
Debug 8.472 false org.hibernate.connection.DriverManagerConnectionProvider total checked-out connections: 0
Debug 8.462 false org.hibernate.jdbc.ConnectionManager opening JDBC connection
Debug 8.462 false org.hibernate.transaction.JDBCTransaction begin
if you read the line starting with
Debug 8.532 hql is : select user.userName, user.email, user.password from User as user where user.userName = 'Kesinleş' . At hqls where clause there is a Turkish char (similar to s in case you cant see properly). And above the line that starts with [b]Debug 8.693[b] in Ast trees last part Turkish chars converted to ? .
Should i add an issue to jira?
Thanks
Selim