I'm trying to migrate to Hibernate 3.0.3. I use as DB Mycrosoft SQL Server.
I have a table with a param_id primary key, bigint(8). I use the following mapping file for this table:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.vo.Parameter" table="parameters">
<!-- It's automatically generated by Hibernate with increment class. -->
<id name="paramId" type="long">
<column name="param_id" sql-type="bigint(8)" not-null="true"/>
<generator class="identity"/>
</id>
<property name="paramName">
<column name="param_name" sql-type="varchar(200)" not-null="true"/>
</property>
<property name="paramMin">
<column name="param_min" sql-type="varchar(50)"/>
</property>
<property name="paramMax">
<column name="param_max" sql-type="varchar(50)"/>
</property>
<property name="paramVal">
<column name="param_val" sql-type="varchar(50)"/>
</property>
</class>
</hibernate-mapping>
The class Parameter.java looks like:
package com.vo;
import java.io.Serializable;
public class Parameter implements Serializable{
private long paramId;
private String paramName;
private String paramMin;
private String paramMax;
private String paramVal;
/**
* @return
*/
public String getParamVal() {
return paramVal;
}
/**
* @return
*/
public long getParamId() {
return paramId;
}
/**
* @return
*/
public String getParamMax() {
return paramMax;
}
/**
* @return
*/
public String getParamMin() {
return paramMin;
}
/**
* @return
*/
public String getParamName() {
return paramName;
}
/**
* @param string
*/
public void setParamVal(String string) {
paramVal = string;
}
/**
* @param l
*/
public void setParamId(long l) {
paramId = l;
}
/**
* @param string
*/
public void setParamMax(String string) {
paramMax = string;
}
/**
* @param string
*/
public void setParamMin(String string) {
paramMin = string;
}
/**
* @param string
*/
public void setParamName(String string) {
paramName = string;
}
}
and the ParameterDAO looks like
package com.irom.dao;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.irom.exceptions.InfrastructureException;
import com.irom.utils.HibernateUtil;
import com.irom.vo.Parameter;
import java.util.List;
public class ParameterDAO {
private static Log log = LogFactory.getLog(ContractDAO.class);
public ParameterDAO (){
HibernateUtil.beginTransaction();
}
public Parameter insert(Parameter param) throws InfrastructureException {
try {
param = (Parameter)HibernateUtil.getSession().save(param);//.saveOrUpdateCopy(param);
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
log.error(ex.toString());
HibernateUtil.rollbackTransaction();
throw new InfrastructureException(ex.toString());
} finally {
HibernateUtil.closeSession();
}
return param;
}
public void update(Parameter param) throws InfrastructureException {
try {
HibernateUtil.getSession().saveOrUpdate(param);
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
log.error(ex.toString());
HibernateUtil.rollbackTransaction();
throw new InfrastructureException(ex.toString());
} finally {
HibernateUtil.closeSession();
}
}
public void delete(Parameter param) throws InfrastructureException {
try {
HibernateUtil.getSession().delete(param);
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
log.error(ex.toString());
HibernateUtil.rollbackTransaction();
throw new InfrastructureException(ex.toString());
} finally {
HibernateUtil.closeSession();
}
}
public List getAllParams()
throws InfrastructureException
{
Session session = HibernateUtil.getSession();
List results = null;
try
{
Criteria crit = session.createCriteria(com.irom.vo.Parameter.class);
results = crit.list();
}
catch(HibernateException ex)
{
log.error(ex.toString());
throw new InfrastructureException(ex.toString());
}
return results;
}
public Parameter getParameterById(Long paramId, boolean lock) throws InfrastructureException {
Session session = HibernateUtil.getSession();
Parameter param = null;
try {
if (lock) {
param = (Parameter) session.load(Parameter.class, paramId, LockMode.UPGRADE);
} else {
param = (Parameter) session.load(Parameter.class, paramId);
}
//HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
log.error(ex.toString());
//HibernateUtil.rollbackTransaction();
throw new InfrastructureException(ex.toString());
} finally {
//HibernateUtil.closeSession();
}
return param;
}
}
The update and delete works fine, but when I try to insert I get the following exception:
<B>java.lang.ClassCastException: java.lang.Long </B>
Is the error in the mapping
<id name="paramId" type="long">
<column name="param_id" sql-type="bigint(8)" not-null="true"/>
<generator class="identity"/>
</id>
What's the corresponding generator for the primary key in <b>Hibernate 3.0.3</b>?
I'm waiting for your answers and opinions.[/img]
|