-->
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.  [ 3 posts ] 
Author Message
 Post subject: Migrate to Hibernate 3.0.3
PostPosted: Tue May 24, 2005 5:17 am 
Newbie

Joined: Mon May 23, 2005 10:43 am
Posts: 2
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]


Top
 Profile  
 
 Post subject: probe this.....
PostPosted: Tue May 24, 2005 5:22 am 
Newbie

Joined: Tue May 24, 2005 5:08 am
Posts: 6
did you probe "INCREMENT"?

good luck.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 5:40 am 
Newbie

Joined: Mon May 23, 2005 10:43 am
Posts: 2
I get the same error with "increment" too.

I am forced to migrate to Hibernate 3, in order to call procedures.

Give me other clues!

Klavdia


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