-->
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.  [ 6 posts ] 
Author Message
 Post subject: java.lang.ClassCastException: java.math.BigInteger for long
PostPosted: Fri Apr 28, 2006 1:21 pm 
Newbie

Joined: Fri Apr 28, 2006 1:09 pm
Posts: 9
I'm a hibernate newbie, but I've been through the docs and have been reading the Pro Hibernate 3 book, so I'm hoping this is nothing too dumb.

I've been writing a simple feed reader test application and I'm trying to write an application which will count the number of FeedSubscriptions for a specific feed and store it the corresponding FeedDetails object. The only problem is I hit an exception whenever I try to set the count via the FeedDetails setter method, and the value is not updated. The setter for the relevant field takes a long (i.e. setSubscriberCount(long newCount)), the value passed to the setter is a long, yet the exception is thrown and the count is not update (note: long as in primitive, not Long as in the object).

Code and mapping files below with a comment showing the line at which the exception is thrown.

Any help/pointers appricated.

Al.




Hibernate version:

3.1.3

Mapping documents:

FeedDetails

Code:
<?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>

    <class name="info.rss2mail.objects.FeedDetails">
        <id name="id">
            <generator class="native"/>
        </id>
      <property name="url" unique="true" index="feed_url_idx" />
      <property name="editorEmail" />
      <property name="blocked" />
      <property name="title" />
      <property name="lastScanned"/>
      <property name="nextScanDue"/>
      <property name="lastScanCode"/>
      <property name="subscriberCount"/>

       <set name="feedItems" table="FEED_ITEMS_MAP" lazy="true">
           <key column="feed_id"/>
           <one-to-many class="info.rss2mail.objects.FeedItem"/>
       </set>
               
    </class>

</hibernate-mapping>


Feed Subscription

Code:
<?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>

    <class name="info.rss2mail.objects.FeedSubscription">
        <id name="id">
            <generator class="native"/>
        </id>
      <property name="subscriptionLevel"/>
      <property name="subscriptionTime"/>
      <property name="lastMailingTime"/>
      <property name="active"/>

      <many-to-one   name="user"
                  lazy="false"
                  class="info.rss2mail.objects.User" />

      <many-to-one   name="feedDetails"
                   lazy="false"
                   class="info.rss2mail.objects.FeedDetails" />
    </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
      Transaction tx = null;
      try {
         tx = session.beginTransaction();

         int flushCount = 0;
         
         
         SQLQuery query = session.createSQLQuery("select id, count(*) from FeedSubscription group by id");
         List results = query.list();
          Iterator iter = results.iterator();
          while(iter.hasNext()) {
             Object[] data = (Object[]) iter.next();
             
             FeedDetails theFeed = (FeedDetails) session.load(FeedDetails.class, (Number)data[0]);
             long value = ((Number)data[1]).longValue();
/*EXCEPTION HERE:*/   theFeed.setSubscriberCount(value);
             System.out.println(theFeed.getId()+":"+value);
             session.update(theFeed);
             
             flushCount++;
             if(flushCount == 10) {
                session.flush();
             }
          }
         
         tx.commit();
         tx = null;
      } finally {
         if( tx != null ) {
            tx.rollback();
         }
      }



Full stack trace of any exception that occurs:

Exception in thread "main" java.lang.ClassCastException: java.math.BigInteger
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:82)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:820)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:62)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at info.rss2mail.objects.FeedDetails$$EnhancerByCGLIB$$69b7ee90.setSubscriberCount(<generated>)
at info.rss2mail.backend.feedranker.FeedRanker.main(FeedRanker.java:52)


Name and version of the database you are using:

MySQL 3.23

The generated SQL (show_sql=true):

Hibernate: select id, count(*) from FeedSubscription group by id
Hibernate: select feeddetail0_.id as id0_0_, feeddetail0_.url as url0_0_, feeddetail0_.editorEmail as editorEm3_0_0_, feeddetail0_.blocked as blocked0_0_, feeddetail0_.title as title0_0_, feeddetail0_.lastScanned as lastScan6_0_0_, feeddetail0_.nextScanDue as nextScan7_0_0_, feeddetail0_.lastScanCode as lastScan8_0_0_, feeddetail0_.subscriberCount as subscrib9_0_0_ from FeedDetails feeddetail0_ where feeddetail0_.id=?

Debug level Hibernate log excerpt:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select id, count(*) from FeedSubscription group by id
Hibernate: select feeddetail0_.id as id0_0_, feeddetail0_.url as url0_0_, feeddetail0_.editorEmail as editorEm3_0_0_, feeddetail0_.blocked as blocked0_0_, feeddetail0_.title as title0_0_, feeddetail0_.lastScanned as lastScan6_0_0_, feeddetail0_.nextScanDue as nextScan7_0_0_, feeddetail0_.lastScanCode as lastScan8_0_0_, feeddetail0_.subscriberCount as subscrib9_0_0_ from FeedDetails feeddetail0_ where feeddetail0_.id=?
Exception in thread "main" java.lang.ClassCastException: java.math.BigInteger
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:82)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:820)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:62)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at info.rss2mail.objects.FeedDetails$$EnhancerByCGLIB$$69b7ee90.setSubscriberCount(<generated>)
at info.rss2mail.backend.feedranker.FeedRanker.main(FeedRanker.java:52)


Top
 Profile  
 
 Post subject: I guess
PostPosted: Fri Apr 28, 2006 2:22 pm 
Regular
Regular

Joined: Wed Feb 22, 2006 11:28 am
Posts: 65
Location: Santiago, Chile
Hello Friend:

After reading ur problem:

A. Can you post your JAVA CODE of that Object [b]"FeedDetails"[/b]
B. I think the problem line site: long value = ((Number)data[1]).longValue();

You can try to cast data[1] like this:
[u]long value = Long.parseLong(String.valueOf(data[1]));[/u]

I hope that can help u.

Requests for comments.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 3:21 pm 
Newbie

Joined: Fri Apr 28, 2006 1:09 pm
Posts: 9
Many thanks for the ideas, unfortunatley the combination of Long and String methods didn't work, it leaves me with the same exception :(.

The code you asked for is as follows;

Code:
package info.rss2mail.objects;

import java.util.Set;

public class FeedDetails {

   private Long id;
   
   private String url;
   private String editorEmail;
   private Boolean blocked;
   private String title;
   private long lastScanned;
   private long nextScanDue;
   private int lastScanCode;
   private long subscriberCount;
   
   private Set feedItems;
   
   public Boolean getBlocked() {
      return blocked;
   }
   public void setBlocked(Boolean blocked) {
      this.blocked = blocked;
   }
   public String getEditorEmail() {
      return editorEmail;
   }
   public void setEditorEmail(String editorEmail) {
      this.editorEmail = editorEmail;
   }
   public Long getId() {
      return id;
   }
   protected void setId(Long id) {
      this.id = id;
   }
   public int getLastScanCode() {
      return lastScanCode;
   }
   public void setLastScanCode(int lastScanCode) {
      this.lastScanCode = lastScanCode;
   }
   public long getLastScanned() {
      return lastScanned;
   }
   public void setLastScanned(long lastScanned) {
      this.lastScanned = lastScanned;
   }
   public String getTitle() {
      return title;
   }
   public void setTitle(String newTitle) {
      title = newTitle;
   }
   public long getNextScanDue() {
      return nextScanDue;
   }
   public void setNextScanDue(long nextScanDue) {
      this.nextScanDue = nextScanDue;
   }
   public String getUrl() {
      return url;
   }
   public void setUrl(String url) {
      this.url = url;
   }
   public long getSubscriberCount() {
      return subscriberCount;
   }
   public void setSubscriberCount(long count) {
      subscriberCount = count;
   }

   /**
    * Get the list of items from this feed.
    */
   
   public Set getFeedItems() {
      return feedItems;
   }

   /**
    * Set the list of items for this feed.
    */
   
   public void setFeedItems( Set newFeedItems ) {
      feedItems = newFeedItems;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 29, 2006 5:39 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
could you please make change as follows and try

<property name="subscriberCount" type="long"/>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 29, 2006 9:02 am 
Newbie

Joined: Fri Apr 28, 2006 1:09 pm
Posts: 9
Thanks for the idea, but still not joy :(, same exception, same place.


Top
 Profile  
 
 Post subject: Problem Solved
PostPosted: Sat Apr 29, 2006 11:38 am 
Newbie

Joined: Fri Apr 28, 2006 1:09 pm
Posts: 9
After some playing around I've found a fix, it appears that the exception is actually caused by the id passed to the load method (I'm guessing that the object is only fetched when I call the setter despite the code saying reading as if it's called earlier).

This means that the following code works;

Code:
Long feedId = new Long(((Number)data[0]).longValue());
FeedDetails theFeed = (FeedDetails) session.load(FeedDetails.class, feedId);
long value = Long.parseLong(String.valueOf(data[1]));
theFeed.setSubscriberCount(value);


It seems strange that the object returned for the ID column isn't actually an object compatible with the ID column, but I guess every system has it's funnies.

Many thanks to everyone who spent time looking at this and thinking about it.

Al.


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