-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Issue getting UserType to work
PostPosted: Mon Sep 22, 2008 11:00 am 
Newbie

Joined: Mon Sep 22, 2008 9:31 am
Posts: 9
Hibernate version: 3.2.5

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: MySQL Ver. 14.12 Distrib 5.0.15 for Win 32


Hi Everyone,
I seem to be having an issue using a custom UserType I created to handle java.net.URI objects. Prior to implementing a UserType the value is stored in the database as a MySQL type VarBinary. I want this value to be stored as a VarChar; So, I attempted to create a UserType to resolve my need. After creating a custom UserType to store the URI object as a Hibernate.STRING and retrieve a Hibernate.STRING as a java.net.URI object, the database is still populated with a datatype of VarBinary. I debugged the code and noticed that my custom UserType's nullSafeSet is never called (at this time I am just trying to populate the database, no retrieval is being done yet.) I've been running the application and debugging the application within my Eclipse IDE.

Here is the mapping, and class:

Mapping File Snippet:
Code:
<class entity-name="DocumentData" table="tbl_documents">
<id name="executionId" type="integer" />
<component name="doc" class="com.coname.Document" />
</class>

<class name="com.coname.Document" table="tbs_documents">
<id name="docId">
  <generator class="native" />
</id>
<property name="uri" type="com.coname.persistence.hibernate.usertypes.URIUserType" />
<property name="executionId"/>
</class>


Java Class UserType Snippet:
Code:
public class URIUserType implements org.hibernate.usertype.UserType{
public Object nullSafeGet(ResultSet rs, String[] uris, Object owner) throws HibernateException, SQLException{
  String uriString = (String) Hibernate.STRING.nullSafeGet(rs, uris[0]);

  if(uriString == null){
   return null;
  }
 
  try{
   return new java.net.URI(uriString);
  }
  catch...
}

public void nullSafeSet(PreparedStatement stmt, Object value, int index) throws HibernateException, SQLException{
  String uriToString = null;

  if(value != null){
   uriToString = ((java.net.URI) value).toString();
   
   Hibernate.STRING.nullSafeSet(stmt, uriToString, index);
  }
}
...Other implemented methods required by org.hibernate.usertype.UserType



Calling Hibernate Java Snippet:
Code:
HashMap<String, Object> docMap = new HashMap<String, Object>();
docMap.put("executionId", workflowExecutionId);//workflowExecutionId is a variable set by Hibernate in a previous step.
docMap.put("doc", doc);//doc is a Document object that has been declared and populated.

session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save("DocumentData", docMap);
//I've also tried to call session.flush() prior to calling commit.
session.getTransaction().commit();


My database is getting populated as expected with all of the other values, it is just the URI that I am attempting to save never goes through the URIUserType that I created and is saved in what I presume is the default VarBinary data type. I've also gone as far as to create a CompositeUserType doing the same thing with no success either. Any guidance would be greatly appreciated. Please let me know if I've left out any crucial or pertinent information. Thanks for your time.


Top
 Profile  
 
 Post subject: Additional information for Issue relating to UserType
PostPosted: Mon Sep 22, 2008 1:01 pm 
Newbie

Joined: Mon Sep 22, 2008 9:31 am
Posts: 9
O.k... If I call Hibernate Session's save(Object) method passing in the Document object, the nullSafeSet call fires as it should. However, when I call the mapped entity "DocumentData", it seems to lose the fact that the component "doc" is mapped to a concrete class mapping in one way (that being the custom UserType type mapping for the parameter "uri".)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.