In order to investigate whether Hibernate's behavior is different than NHibernate's for a particular usage scenario, I began writing a Java SE & Hibernate port of the relevant parts of my NHibernate-based application, but encountered a problem with obtaining an entity where the corresponding SQL Server 2008 R2 Express table uses a uniqueidentifier
for the primary key.
Here's the mapping file:
<!-- User.hbm.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernatetest.entity.User" table="aspnet_Users">
<id name="Id">
<column name="UserId" sql-type="uniqueidentifier" not-null="true" />
</id>
<!-- ... -->
And corresponding POJO definition:
// hibernatetest/entity/User.java
package hibernatetest.entity;
import java.util.UUID;
public class User {
private UUID id;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
//...
And main code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
User currentUser = (User) session.get(User.class, UUID.fromString("473D248D-8D91-41C9-BD73-8ACA45539D79"));
The problem is that the call to Session#get returns null
, as it fails to find the user entity for primary key '473D248D-8D91-41C9-BD73-8ACA45539D79'
.
When I enable full logging, I see:
DEBUG [main] (AbstractBatcher.java:401) - select user0_.UserId as UserId0_0_ from aspnet_Users user0_ where user0_.UserId=?
Hibernate: select user0_.UserId as UserId0_0_ from aspnet_Users user0_ where user0_.UserId=?
TRACE [main] (NullableType.java:133) - binding '2c6d8085f3f2808eeae1f6e1aef5f4e9ecaed5d5c9c43c19837718ed05af828082ca808cece5e1f3f4d3e9e7c2e9f4f3ca808bedeff3f4d3e9e7c2e9f4f3f8f03df30a4ac5d31df9c7bda40d0d11c149' to parameter: 1
I don't know how Hibernate derived the value '2c6d8085f...
, but this is not the value binding that I expected, and it prevents the user entity from being located.
If I change the type of the id
field of hibernatetest.entity.User
from java.util.UUID
to String
, then this line succeeds in finding the entity:
User currentUser = (User) session.get(User.class, "473D248D-8D91-41C9-BD73-8ACA45539D79");
And the log output becomes:
DEBUG [main] (AbstractBatcher.java:401) - select user0_.UserId as UserId0_0_ from aspnet_Users user0_ where user0_.UserId=?
Hibernate: select user0_.UserId as UserId0_0_ from aspnet_Users user0_ where user0_.UserId=?
TRACE [main] (NullableType.java:133) - binding '473D248D-8D91-41C9-BD73-8ACA45539D79' to parameter: 1
How did Hibernate calculate the value '2c6d8085f...
to bind for the UUID '473D248D-8D91-41C9-BD73-8ACA45539D79'
? How can I make it bind the actual UUID value instead?