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 SubscriptionCode:
<?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)