-->
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.  [ 6 posts ] 
Author Message
 Post subject: UserType configuration in HQL Editor
PostPosted: Fri Nov 30, 2007 6:53 am 
Newbie

Joined: Wed Nov 24, 2004 2:44 am
Posts: 11
Hibernate version:
Hibernate Eclipse plugin 3.2.0 CR1

My application is connecting to an Oracle database with Hibernate 3.2.5 GA where dates are stored as string (VARCHAR), in format "yyyyMMdd".

In order to have these dates in my Pojos as java.util.Date, I've implemented both interfaces UserType and ParameterizedType. This type takes one parameter: "datePattern" (default value: "yyyyMMdd").

Now I'm using HQL Editor available with Hibernate Eclipse plugin for development purposes and I'd like to configure this UserType/ParameterizedType in the Editor. Otherwise, I couldn't test any HQL statement on my pojo attributes using this type (eg. select an entity where one entity attribute of this type is equal to the system date).

So how can I configure this UserType in HQL Editor ? This seems to be available for all native Hibernate types (tab "Query Parameters", column "type") but not for user defined types.

Thank for your help,

Bertrand


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 9:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what happens when you click the :P+ when you have a query with parameters for your custom type ?

It should automatically figure out the type.

if that doesn't work then report it in jira.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 10:00 am 
Newbie

Joined: Wed Nov 24, 2004 2:44 am
Posts: 11
max wrote:
what happens when you click the :P+ when you have a query with parameters for your custom type ?

It should automatically figure out the type.


Hereby you will find all the necessary details for my use case....

Statement.java
Code:
package com.mainsys.account.statement.model;

import java.util.Date;

public class Statement implements java.io.Serializable {

     private long id;
     private Date lastProductionDate;

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getLastProductionDate() {
        return this.lastProductionDate;
    }

    public void setLastProductionDate(Date lastPoductionDate) {
        this.lastProductionDate = lastPoductionDate;
    }

}


StringDateType.java
Code:
package com.mainsys.account.statement.dao.hibernate;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

public class StringDateType implements UserType,ParameterizedType {

   protected Log log = LogFactory.getLog(this.getClass().getName());

   public int[] sqlTypes() {
       return new int[] { Types.VARCHAR };
     }

   private String datePattern = "yyyyMMdd";

   public Class returnedClass() {
      return java.util.Date.class;
   }

   public void setParameterValues(Properties arg0) {
      
      String pattern = arg0.getProperty("datePattern");
      if (pattern != null) {
         this.datePattern = pattern;
      }
      
   }

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

    public boolean isMutable() {
        return false;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
       if (x == y)
          return true;
       else if (x != null && x.equals(y))
          return true;
       else
          return false;
    }

    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

   public Object nullSafeGet(ResultSet arg0, String[] names, Object arg2) throws HibernateException, SQLException {

      SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getInstance();
      formatter.applyPattern(datePattern);

      Date result = null;

      String dateAsString = arg0.getString(names[0]);

      if (!arg0.wasNull()) {
          try {
            result = "".equals(dateAsString) ? null : formatter.parse(dateAsString);
         } catch (ParseException e) {
            log.error("Unable to parse date",e);
            throw new HibernateException("Unable to parse date",e);
         }
      }
      
      return result;
   }

   public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2) throws HibernateException, SQLException {
      log.fatal("This method is not implemented");      
      throw new HibernateException("This method is not implemented");
   }
   
}


Statement.hbm.xml
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="com.mainsys.account.statement.model">

   <class name="Statement" table="STATEMENT" abstract="true">
   
      <id name="id" type="long"
         <column name="STATEMENT_ID" precision="12" scale="0" not-null="true" />
         <generator class="sequence">
            <param name="sequence">SEQ_STATEMENT</param>
           </generator>
      </id>
      
                <property name="date" type="StringDateType">
            <column name="DELIVERY_DATE" not-null="true" />
      
   </class>
   
</hibernate-mapping>


TypeDef.hbm.xml
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>   
    <typedef class="com.mainsys.account.statement.dao.hibernate.StringDateType" name="StringDateType">
        <param name="datePattern">yyyyMMdd</param>
    </typedef>
</hibernate-mapping>


STATEMENT.sql
Code:
  CREATE TABLE "STT"."STATEMENT"
   (   "STATEMENT_ID" NUMBER(12,0) NOT NULL ENABLE,
   "LAST_PRODUCTION_DATE" VARCHAR2(8 BYTE) NOT NULL ENABLE,
    CONSTRAINT "STATEMENT_01" PRIMARY KEY ("STATEMENT_ID")
   );


I'm trying to execute the following statement in HQL Editor:

Code:
from Statement stt where stt.lastProductionDate = :businessDate


When I click the :P+, HQL Editor displays:
- Name: businessDate
- Type: string

I don't really know if it should display those parameter values or the following ones instead:
- Name: businessDate
- Type: StringDateType

Moreover, what value should I provide to the parameter within HQL Editor ? A java.lang.String or a java.util.Date ?

I've got the same issue with a generic enum UserType.


Bertrand


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 10:05 am 
Newbie

Joined: Wed Nov 24, 2004 2:44 am
Posts: 11
Please use this correct mapping file for Statement Entity (there are errors in the previous one).

Statement.hbm.xml
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="com.mainsys.account.statement.model">

   <class name="Statement" table="STATEMENT" abstract="true">
   
      <id name="id" type="long"
         <column name="STATEMENT_ID" precision="12" scale="0" not-null="true" />
         <generator class="sequence">
            <param name="sequence">SEQ_STATEMENT</param>
           </generator>
      </id>
     
      <property name="lastProductionDate" type="StringDateType">
            <column name="LAST_PRODUCTION_DATE" not-null="true" />
      </property>     

   </class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 30, 2007 10:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please report it in jira.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 01, 2007 11:25 am 
Newbie

Joined: Wed Nov 24, 2004 2:44 am
Posts: 11
max wrote:
please report it in jira.


Issue reported in Jira HBX-1019


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