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.javaCode:
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.xmlCode:
<?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.xmlCode:
<?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.sqlCode:
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