-->
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.  [ 4 posts ] 
Author Message
 Post subject: Need Examples/Help Using a UserType (not defining one)
PostPosted: Fri Jul 08, 2005 1:12 am 
Newbie

Joined: Fri Jan 07, 2005 3:34 pm
Posts: 8
Hi,

I've been able to find many examples of implementations UserTypes but not any examples of using user types. If you could link me to any example code on using (not defining) a UserType, that would be a big help.

Here's some pseudocode of what I am *trying* to do by using the the “StringClobType”, defined @ http://www.hibernate.org/56.html

suppose I want to load a row that has a clob column and i want to change it and then save it:
MyEntity obj = session.load(MyEntity.class, <primarykey>);
StringClobType stringClob = obj.getMyClobProperty();
//// At this point how am I going to get or change the contents of stringClob ????
obj.setMyClobProperty(stringClob);
session.update(obj);

suppose I want instantiate a new row, and save my clob:
StringClobType newClob = ?????; /// at this point how am i going to instantiate a new StringClobType
MyEntity obj = new MyEntity(..., ..., ...newClob)
session.save(obj);

Derrill

Hibernate version:
2
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:



Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 1:25 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
for me it sounds like you habe a little misunderstanding in using a UserType ...

You shouldn't use it as a returntype of your bean ... a 'normal' HibernateType isn't used that way.

In your case the UserType may return a String or another (custom) Java-Type ...

Besides that, it would be better showing your real code and mapping ... and not "pseudocode" ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 10:59 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Heres some small example

My Java-Class:
Code:
public class User {
   private String userID;
   private Date lastLogon;
}


My Mapping file:

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>
    <class
        name="my.User"
        table="users"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="ID"
            column="LogonId"
            type="java.lang.String"
        >
            <generator class="assigned">
            </generator>
        </id>
        <property
            name="lastLogon"
            type="my.DateType"
            update="true"
            insert="true"
            access="property"
            column="lastLogon"
        />
    </class>

</hibernate-mapping>

My UserType:

Code:
public class DateType
   implements UserType {
   private static final int[] SQL_TYPES = {Types.CHAR};
   private int dateFormat;
   private int dateFormatInsert;

   public DateType() {
      super();
      setDateFormat(IfbHibernate.getDateTimeFormats().getDateFormat());
      setDateFormatInsert(IfbHibernate.getDateTimeFormats()
            .getDateFormatInsert());
   }

   public int[] sqlTypes() {
      return SQL_TYPES;
   }
   public boolean equals(Object x, Object y) throws HibernateException {
      if (x == y)
         return true;
      if (x == null || y == null)
         return false;
      return x.equals(y);
   }

   public Object deepCopy(Object obj) throws HibernateException {
      return obj;
   }

   public boolean isMutable() {
      return false;
   }

   public void nullSafeSet(java.sql.PreparedStatement statement, Object value,
      int index) throws HibernateException, java.sql.SQLException {
      if (value == null) {
         statement.setNull(index, sqlTypes()[0]);
      }
      else {
         Date date = (Date) value;
         String str = DBDateTimeFormats.toDBDate(date, getDateFormatInsert());
         statement.setString(index, str);
      }
   }

   public Object nullSafeGet(java.sql.ResultSet resultSet, String[] names,
      Object owner) throws HibernateException, java.sql.SQLException {
      String str = resultSet.getString(names[0]);
      if (resultSet.wasNull())
         return null;
      try {
         return new Date(str, getDateFormat());
      }
      catch (Exception e) {
         throw new HibernateException(
               "DateException1: " + e.getLocalizedMessage()); //$NON-NLS-1$
      }
   }

   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#returnedClass()
    */
   public Class returnedClass() {
      return Date.class;
   }
   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
    */
   public int hashCode(Object x) throws HibernateException {
      return ((Date) x).hashCode();
   }
   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
    */
   public Serializable disassemble(Object value) throws HibernateException {
      return (Serializable) value;
   }
   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
    */
   public Object assemble(Serializable cached, Object owner)
         throws HibernateException {
      return cached;
   }
   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
    */
   public Object replace(Object original, Object target, Object owner)
         throws HibernateException {
      return original;
   }

   public int getDateFormat() {
      return dateFormat;
   }

   private void setDateFormat(int aDateFormat) {
      this.dateFormat = aDateFormat;
   }

   public int getDateFormatInsert() {
      return dateFormatInsert;
   }

   private void setDateFormatInsert(int aDateFormatInsert) {
      this.dateFormatInsert = aDateFormatInsert;
   }

}

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 10:28 pm 
Newbie

Joined: Fri Jan 07, 2005 3:34 pm
Posts: 8
Ah OK. I understand now.

I was thinking about how I instantiate and use the UserType in my code, but really the UserType is not used directly in code but instead used in the mapping file as a property type.
The UserType definition provides a conversion from the SQL type to to the returned (Java) type.

Thanks for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.