-->
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: Getting ClassCastException on id for session.save
PostPosted: Wed Apr 19, 2006 5:08 pm 
Newbie

Joined: Fri Jul 15, 2005 6:50 pm
Posts: 3
Location: Bay Area, California,USA
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Getting ClassCastException for the id. Please look at the following Order.java snippet. I have xdoclet annotations and I have also posted the Order.hbm generated using the xdoclet annotations.
<b>----------------------- -Order.java------------------------------------</b>
/**
* @hibernate.class table="MSL_ORDER" schema="MSL"
*/
public class Order extends BaseEntity {

/** order reference id */
/**
* @hibernate.id column="ORDER_REF_ID" type="long" generator-class="sequence"
* @hibernate.generator-param name="sequence" value="SEQ_MSL_ORDER_REF_ID"
*/
protected long orderRefId;

/** oracle order id
* @hibernate.property column="MSL_ORDER_NUMBER" type="string" length="100"
*
*/
protected String orderId;

/** Source Id ( e.g vip/ ctc)
* @hibernate.property column="SOURCE_ID" type="string" length="50"
*/
protected String sourceId;

/** Order status
* @hibernate.property column="STATUS" type="string" length="30"
*/
protected String status;

/** Final Price
* @hibernate.property column="FINAL_PRICE" type="double"
*/
}
-------------------end ---- -Order.java------------------------------------

<b>--------------------Order.hbm.xml------------------------------------</b>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="MSL_ORDER" name="com.verisign.webapps.services.common.Order" schema="MSL">
<id type="long" column="ORDER_REF_ID" access="field" name="orderRefId">
<generator class="sequence">
<param name="sequence">SEQ_MSL_ORDER_REF_ID</param>
</generator>
</id>
<property name="orderNumber" length="100" type="string" access="field" column="MSL_ORDER_NUMBER"/>
<property name="sourceId" length="50" type="string" access="field" column="SOURCE_ID"/>
<property name="status" length="30" type="string" access="field" column="STATUS"/>
<property name="finalPrice" type="double" access="field" column="FINAL_PRICE"/>
<property name="tax" type="double" access="field" column="TAX"/>
<property name="promoCode" length="30" type="string" access="field" column="PROMO_CODE"/>
<property name="currency" length="10" type="string" access="field" column="CURRENCY"/>
<property name="priceListName" length="240" type="string" access="field" column="PRICE_LIST_NAME"/>
<many-to-one column="CUSTOMER_REF_ID" foreign-key="FK_ORDER_CUSTOMER" access="field" cascade="save-update,persist" name="customer" class="com.verisign.webapps.services.common.Customer"/>
<many-to-one column="PAYMENT_REF_ID" foreign-key="FK_ORDER_PAYMENT" access="field" cascade="save-update,persist" name="payment" class="com.verisign.webapps.services.common.Payment"/>
<many-to-one column="SHIPPING_METHOD_REF_ID" foreign-key="FK_ORDER_SHIPPING_METHOD" access="field" cascade="save-update,persist" name="shippingMethod" class="com.verisign.webapps.services.common.ShippingMethod"/>
<list table="ORDER_PRODUCT" access="field" cascade="save-update,persist" name="products">
<key column="ORDER_REF_ID"/>
<list-index base="0" column="ORDER_PRODUCT_REF_ID"/>
<many-to-many column="PRODUCT_REF_ID" unique="true" class="com.verisign.webapps.services.common.Product"/>
</list>
<set access="field" cascade="none" name="processingErrors">
<key column="ERROR_REF_ID"/>
<one-to-many class="com.verisign.webapps.services.common.ProcessingError"/>
</set>
<property name="createdBy" length="50" type="string" access="field" column="CREATED_BY" update="false"/>
<property name="createDate" length="15" type="timestamp" access="field" column="CREATED_DATE" update="false"/>
<property name="updatedBy" length="50" type="string" access="field" column="UPDATED_BY"/>
<property name="updateDate" length="15" type="timestamp" access="field" column="UPDATED_DATE"/>
</class>
</hibernate-mapping>
-------------------end ---- -Order.hbm.xml------------------------------------


[b]Hibernate version:[/b] 3.1rc2 from the maven site.

[b]Mapping documents:[/b]

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="MSLSessionFactory">
<!-- connection properties -->
<property name="connection.datasource">MSL_TX_DS</property>

<!-- dialect for Oracle (any version) -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.jndi.url">t3://localhost:7001</property>
<property name="hibernate.jndi.class">weblogic.jndi.WLInitialContextFactory</property>

<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.use_outer_join">false</property>
<property name="hibernate.connection.autocommit">false</property>


<mapping resource="Address.hbm.xml" />
<mapping resource="Billing.hbm.xml" />
<mapping resource="Contact.hbm.xml" />
<mapping resource="Customer.hbm.xml" />
<mapping resource="Order.hbm.xml" />
<mapping resource="Payment.hbm.xml" />
<mapping resource="ProcessingError.hbm.xml" />
<mapping resource="Product.hbm.xml" />
<mapping resource="Shipping.hbm.xml" />
<mapping resource="ShippingMethod.hbm.xml" />
</session-factory>
</hibernate-configuration>

[b]Code between sessionFactory.openSession() and session.close():[/b]
session.save(order);

[b]Full stack trace of any exception that occurs:[/b]

java.llang.ClassCastException: java.lang.Long

The exception is thrown from the session.save call

[b]Name and version of the database you are using:[/b] Oracle 9i

[b]The generated SQL (show_sql=true):[/b]
Hibernate: select SEQ_MSL_ORDER_REF_ID.nextval from dual

[b]Debug level Hibernate log excerpt:[/b]

""<DEBUG> <2006-04-19 14:10:32,133> <impl.SessionImpl> [opened session at timestamp: 4691890307411968]
""<DEBUG> <2006-04-19 14:10:32,135> <transaction.JDBCTransaction> [begin]
""<DEBUG> <2006-04-19 14:10:32,136> <jdbc.ConnectionManager> [opening JDBC connection]
""<DEBUG> <2006-04-19 14:10:32,136> <transaction.JDBCTransaction> [current autocommit status: true]
""<DEBUG> <2006-04-19 14:10:32,136> <transaction.JDBCTransaction> [disabling autocommit]
""<DEBUG> <2006-04-19 14:10:32,136> <jdbc.JDBCContext> [after transaction begin]
""<DEBUG> <2006-04-19 14:10:32,146> <def.DefaultSaveOrUpdateEventListener> [saving transient instance]
""<DEBUG> <2006-04-19 14:10:32,146> <jdbc.AbstractBatcher> [about to open PreparedStatement (open PreparedStatements: 0, globally: 0)]
""<DEBUG> <2006-04-19 14:10:32,146> <hibernate.SQL> [select SEQ_MSL_ORDER_REF_ID.nextval from dual]
""<DEBUG> <2006-04-19 14:10:32,148> <jdbc.AbstractBatcher> [preparing statement]
""<DEBUG> <2006-04-19 14:10:32,150> <id.SequenceGenerator> [Sequence identifier generated: 3]
""<DEBUG> <2006-04-19 14:10:32,150> <jdbc.AbstractBatcher> [about to close PreparedStatement (open PreparedStatements: 1, globally: 1)]
""<DEBUG> <2006-04-19 14:10:32,150> <jdbc.AbstractBatcher> [closing statement]
""<DEBUG> <2006-04-19 14:10:32,150> <def.AbstractSaveEventListener> [generated identifier: 3, using strategy: org.hibernate.id.SequenceGene
rator]
""<DEBUG> <2006-04-19 14:10:32,152> <def.AbstractSaveEventListener> [saving [com.verisign.webapps.services.common.Order#3]]
""<DEBUG> <2006-04-19 14:10:32,158> <engine.Cascade> [processing cascade ACTION_SAVE_UPDATE for: com.verisign.webapps.services.common.Order
]
""<DEBUG> <2006-04-19 14:10:32,158> <engine.Cascade> [done processing cascade ACTION_SAVE_UPDATE for: com.verisign.webapps.services.common.
Order]
""<DEBUG> <2006-04-19 14:10:32,178> <engine.Cascade> [processing cascade ACTION_SAVE_UPDATE for: com.verisign.webapps.services.common.Order
]
""<DEBUG> <2006-04-19 14:10:32,178> <engine.Cascade> [done processing cascade ACTION_SAVE_UPDATE for: com.verisign.webapps.services.common.
Order]
""<DEBUG> <2006-04-19 14:10:32,222> <transaction.JDBCTransaction> [rollback]
""<DEBUG> <2006-04-19 14:10:32,222> <transaction.JDBCTransaction> [re-enabling autocommit]
""<DEBUG> <2006-04-19 14:10:32,222> <transaction.JDBCTransaction> [rolled back JDBC Connection]
""<DEBUG> <2006-04-19 14:10:32,222> <jdbc.JDBCContext> [after transaction completion]
""<DEBUG> <2006-04-19 14:10:32,222> <jdbc.ConnectionManager> [aggressively releasing JDBC connection]
""<DEBUG> <2006-04-19 14:10:32,223> <jdbc.ConnectionManager> [releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open R
esultSets: 0, globally: 0)]]
""<DEBUG> <2006-04-19 14:10:32,225> <impl.SessionImpl> [after transaction completion]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 6:49 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 2:26 pm
Posts: 28
You didn't post a FULL stack trace. Try using a Long instead of the primitive long.


Top
 Profile  
 
 Post subject: Getting ClassCastException on id for session.save
PostPosted: Fri Apr 21, 2006 6:50 am 
Newbie

Joined: Fri Apr 21, 2006 5:44 am
Posts: 3
Location: UK
I had a similar problem when my id field was using an int. Got round this one by writing a method convertToInteger() as Hibernate needs the identifier field to be serializable. Objects are but primitives aren't. Maybe try creating a util method something like:
Code:
   public static Long convertToLong(final long fieldValue) {
       Long returnValue =  new Long(fieldValue);
       return returnValue;
   }

Then you could use it in your DAO something like:
Code:
...

    transaction = session.beginTransaction();
    session.save(UtilClass.convertToLong(orderId));
    transaction.commit();

...

Hope this helps


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.