Hi,
I am new to Hibernate. I am trying to insert record into AuctionUser table in Oracle 9i.
My requirement are,
1. I need to use Oracle Sequence
2. In AuctionUser class, primary key attribute ID needs to be of type BigDecimal.
To achieve this, I have extended SequenceGenerator and overridden generate() method, which returns Sequence value in BigDecimal object.
But when I do this, I am getting ClassCastException in set() method in net.sf.hibernate.type.LongType class.
Details are below.
Please advise.
Thanks
JP
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
2.1.8
AuctionUser.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="hibernate.test.AuctionUser"
table="auctionuser"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.math.BigDecimal"
>
<generator class="hibernate.util.BigDecimalIdGenerator">
<param name="sequence">OracleSequenceName</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-AuctionUser.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="email"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="email"
/>
<property
name="firstname"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="firstname"
/>
<property
name="lastname"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="lastname"
/>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="username"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-AuctionUser.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Session session;
try
{
session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
AuctionUser user = new AuctionUser();
user.setFirstname("FirstName");
user.setLastname("LastName");
user.setEmail("user@company.com");
user.setUsername("userId");
session.save(user);
tx.commit();
HibernateUtil.closeSession();
} catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
out.println("Exception thrown "+e);
}
java.lang.ClassCastException
net.sf.hibernate.type.LongType.set(LongType.java:35)
net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:393)
net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:460)
net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)
net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
com.jayant.scwcd.servlets.SelectBeer.doGet(SelectBeer.java:55)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
public class BigDecimalIdGenerator extends SequenceGenerator
{
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException, SQLException {
Serializable id = super.generate(session,obj);
BigDecimal oid = new BigDecimal(String.valueOf(id));
return oid;
}
}
Oracle 9i
Hibernate: insert into auctionuser (email, firstname, lastname, username, id) values (?, ?, ?, ?, ?)
Debug level Hibernate log excerpt: