Class cast exception loading bean

405 views
Skip to first unread message

Daryl Stultz

unread,
Jul 7, 2014, 4:00:57 PM7/7/14
to eb...@googlegroups.com
Hi Rob,

I'm having a strange "class" of exceptions happening on a high percentage of unit tests involving Ebean 4. The exception is sometimes a NullPointerException, usually a ClassCastException but always involves the "wrong type" of object being set. I'll give a couple abbreviated traceback examples then a full trace.

javax.persistence.PersistenceException: Error loading on com.myproject.webapp.modeleb.ScheduleDay.dayType

Caused by: java.lang.RuntimeException: set dayType on [com.myproject.webapp.modeleb.ScheduleDay] arg[Identifiable-ScheduledAssignment:5] type[com.myproject.webapp.modeleb.ScheduleDay]

Caused by: java.lang.ClassCastException: com.myproject.webapp.modeleb.ScheduledAssignment cannot be cast to com.myproject.webapp.modeleb.DayType


Above, ScheduleDay.dayType is getting a ScheduledAssignment.



javax.persistence.PersistenceException: Error loading on com.myproject.webapp.modeleb.SchedulePeriodAutomation.automation

Caused by: java.lang.RuntimeException: set automation on [com.myproject.webapp.modeleb.SchedulePeriodAutomation] arg[Identifiable-SchedulePeriod:38] type[com.myproject.webapp.modeleb.SchedulePeriodAutomation]

Caused by: java.lang.ClassCastException: com.myproject.webapp.modeleb.SchedulePeriod cannot be cast to com.myproject.webapp.modeleb.Automation

Above SchedulePeriodAutomation.automation is getting a SchedulePeriod.

The failures are mostly repeatable, though I've found one that fails only when it runs after another one that fails. When run alone it passes. I wrote a new test that fails only on a certain record. Whenever a query would return a certain record it fails. That record happens to have a negative primary key value but I tried creating a new record with a different negative PK and it worked fine. So it appears to be data or state dependent in some way. I'm having trouble coming up with some way to pare it down to say exactly what's causing it. Does this problem prompt any quick thoughts from you? Can you suggest how I might work to expose the real problem? I can't find any pattern between classes and records that are failing.


Here's a full traceback example:

javax.persistence.PersistenceException: Error loading on com.myproject.webapp.modeleb.RoleTimeCredit.roleTimeCreditType
 at com
.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:88)
 at com
.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.load(BeanPropertyAssocOne.java:559)

Caused by: java.lang.RuntimeException: set roleTimeCreditType on [com.myproject.webapp.modeleb.RoleTimeCredit] arg[Identifiable-RoleTimeCredit:2] type[com.myproject.webapp.modeleb.RoleTimeCredit] threw error
 at com
.avaje.ebeaninternal.server.deploy.BeanProperty.setValue(BeanProperty.java:708)
 at com
.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.setValue(BeanPropertyAssocOne.java:536)
 at com
.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:79)
 
... 33 more
Caused by: java.lang.ClassCastException: com.myproject.webapp.modeleb.RoleTimeCredit cannot be cast to com.myproject.webapp.modeleb.RoleTimeCreditType
 at com
.myproject.webapp.modeleb.RoleTimeCredit._ebean_setField(RoleTimeCredit.java:1)
 at com
.avaje.ebeaninternal.server.reflect.EnhanceBeanReflect$Setter.set(EnhanceBeanReflect.java:84)
 at com
.avaje.ebeaninternal.server.deploy.BeanProperty.setValue(BeanProperty.java:703)
 
... 35 more

javax
.persistence.PersistenceException: Error loading on com.myproject.webapp.modeleb.SchedulePeriodAutomation.automation
 at com
.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:88)
 at com
.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.load(BeanPropertyAssocOne.java:559)
 at com
.avaje.ebeaninternal.server.query.SqlTreeNodeBean.load(SqlTreeNodeBean.java:236)
 at com
.avaje.ebeaninternal.server.query.CQuery.readRow(CQuery.java:515)
 at com
.avaje.ebeaninternal.server.query.CQuery.readBeanInternal(CQuery.java:542)
 at com
.avaje.ebeaninternal.server.query.CQuery.hasNextBean(CQuery.java:657)
 at com
.avaje.ebeaninternal.server.query.CQuery.readTheRows(CQuery.java:649)
 at com
.avaje.ebeaninternal.server.query.CQuery.readCollection(CQuery.java:615)
 at com
.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:168)
 at com
.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:80)
 at com
.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:251)
 at com
.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1444)
 at com
.avaje.ebeaninternal.server.core.DefaultServer.findUnique(DefaultServer.java:1228)
 at com
.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findUnique(DefaultOrmQuery.java:925)
 at com
.avaje.ebeaninternal.util.DefaultExpressionList.findUnique(DefaultExpressionList.java:194)
 at com
.myproject.webapp.ealeb.SchedulePeriodAutomationAccess.findBySchedulePeriodAndAutomationIds(SchedulePeriodAutomationAccess.java:33)
 at com
.myproject.webapp.ealeb.SchedulePeriodAutomationAccess.find(SchedulePeriodAutomationAccess.java:25)
 at com
.myproject.webapp.unittest.UnitTestSupport.insertAllotments(UnitTestSupport.java:577)
 at com
.myproject.webapp.unittest.UnitTestSupport.insertAllotments(UnitTestSupport.java:497)
 at com
.myproject.webapp.unittest.TestBasicScatter.testAssignmentSorting(TestBasicScatter.java:2255)
 at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun
.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java
.lang.reflect.Method.invoke(Method.java:597)
 at junit
.framework.TestCase.runTest(TestCase.java:176)
 at junit
.framework.TestCase.runBare(TestCase.java:141)
 at junit
.framework.TestResult$1.protect(TestResult.java:122)
 at junit
.framework.TestResult.runProtected(TestResult.java:142)
 at junit
.framework.TestResult.run(TestResult.java:125)
 at junit
.framework.TestCase.run(TestCase.java:129)
 at junit
.framework.TestSuite.runTest(TestSuite.java:255)
 at junit
.framework.TestSuite.run(TestSuite.java:250)
 at junit
.framework.TestSuite.runTest(TestSuite.java:255)
 at junit
.framework.TestSuite.run(TestSuite.java:250)
 at org
.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131)
 at org
.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org
.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org
.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org
.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org
.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: set automation on [com.myproject.webapp.modeleb.SchedulePeriodAutomation] arg[Identifiable-SchedulePeriod:38] type[com.myproject.webapp.modeleb.SchedulePeriodAutomation] threw error
 at com
.avaje.ebeaninternal.server.deploy.BeanProperty.setValue(BeanProperty.java:708)
 at com
.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.setValue(BeanPropertyAssocOne.java:536)
 at com
.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:79)
 
... 39 more
Caused by: java.lang.ClassCastException: com.myproject.webapp.modeleb.SchedulePeriod cannot be cast to com.myproject.webapp.modeleb.Automation
 at com
.myproject.webapp.modeleb.SchedulePeriodAutomation._ebean_setField(SchedulePeriodAutomation.java:1)
 at com
.avaje.ebeaninternal.server.reflect.EnhanceBeanReflect$Setter.set(EnhanceBeanReflect.java:84)
 at com
.avaje.ebeaninternal.server.deploy.BeanProperty.setValue(BeanProperty.java:703)
 
... 41 more


Thanks.

/Daryl

Rob Bygrave

unread,
Jul 8, 2014, 8:42:18 AM7/8/14
to ebean@googlegroups
Is there any inheritance involved?  
Are there any fields in the beans with interesting modifiers?
Are you able to create a reproducing test case?
Are you able to try using Java7?




/Daryl

--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daryl Stultz

unread,
Jul 8, 2014, 8:56:46 AM7/8/14
to eb...@googlegroups.com
On Tue, Jul 8, 2014 at 8:42 AM, Rob Bygrave <robin....@gmail.com> wrote:
Is there any inheritance involved?  

No, at least not necessarily.
 
Are there any fields in the beans with interesting modifiers?

I don't think so, see below.
 
Are you able to create a reproducing test case?

I have determined a pattern. It is data-related. When populating a @ManyToOne relation where the relation has the same primary key value as the root entity it fails. So for these 2 Entities (database has additional columns, not sure that matters):

@Entity
@Table(name = "users", schema = "public")
public class TUser2 {

@Id
@Column(name = "userid")
private Integer id;
@ManyToOne(optional = false)
@JoinColumn(name = "usertypeid")
private TUserType2 userType;

public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
public TUserType2 getUserType() { return this.userType; }
public void setUserType(TUserType2 usertype) { this.userType = usertype; }

}

@Entity
@Table(name = "usertypes", schema = "public")
public class TUserType2 {

@Id
@Column(name = "usertypeid")
private Integer id;
@Version
private int version;
public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
}

This line then fails:

TUser2 user = ebeanServer.find(TUser2.class, 9);

when the TUserType2 primary key is also 9 (but not when userId is different from userTypeId). The reduced stack trace:

javax.persistence.PersistenceException: Error loading on com.opentempo.webapp.modeleb.TUser2.userType
at com.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:88)


Caused by: java.lang.RuntimeException: set userType on [com.opentempo.webapp.modeleb.TUser2] arg[Identifiable-TUser2:9] type[com.opentempo.webapp.modeleb.TUser2] threw error


Caused by: java.lang.ClassCastException: com.opentempo.webapp.modeleb.TUser2 cannot be cast to com.opentempo.webapp.modeleb.TUserType2
at com.opentempo.webapp.modeleb.TUser2._ebean_setField(TUser2.java:1)


So it's passing in user 9 instead of usertype 9. (Not all failures involve the root entity being passed as the relation, though.)

Are you able to try using Java7?

Not presently, but if nothing else pans out I'll try it.

/Daryl 

Rob Bygrave

unread,
Jul 8, 2014, 9:11:29 AM7/8/14
to ebean@googlegroups
Ok, Just tried to reproduce with a quick test and I could not reproduce it.
If you can get a test case that would be great obviously.

Thanks, Rob.


Daryl Stultz

unread,
Jul 8, 2014, 9:33:04 AM7/8/14
to eb...@googlegroups.com
On Tue, Jul 8, 2014 at 9:11 AM, Rob Bygrave <robin....@gmail.com> wrote:
Ok, Just tried to reproduce with a quick test and I could not reproduce it.
If you can get a test case that would be great obviously.

Can you show me what you tried? I have written unit tests for Ebean in the past but it's not real easy for me. I seem to recall creating model classes and somehow the database tables are created automatically, using Derby, I think? I'm not really understanding how the Ebean test setup works.

/Daryl

Rob Bygrave

unread,
Jul 8, 2014, 5:26:04 PM7/8/14
to ebean@googlegroups
For the tests in ebeanorm src/test you just create model entity beans and write a junit test class.  The test should extend BaseTestCase ... so that the agent is automatically loaded to enhance the beans.

... but yes, I'll push the test up tonight. You can alternatively create a small app and email it or put it somewhere.


Cheers, Rob.


Daryl Stultz

unread,
Jul 8, 2014, 8:34:52 PM7/8/14
to eb...@googlegroups.com
On Tue, Jul 8, 2014 at 5:26 PM, Rob Bygrave <robin....@gmail.com> wrote:
You can alternatively create a small app and email it or put it somewhere.

I think I'll take this approach as it might have the best chance of reproducing. 

/Daryl

Daryl Stultz

unread,
Jul 10, 2014, 8:37:19 AM7/10/14
to eb...@googlegroups.com
On Tue, Jul 8, 2014 at 8:42 AM, Rob Bygrave <robin....@gmail.com> wrote:
Is there any inheritance involved?  

I think I misinterpreted this question. There is no "single table inheritance" involved in the failing entities but all of my entities extend a common base class. If I remove the "extends MyBaseClass" from either of two entities involved, the test passes.

I have it reduced to a minimal set of classes but it's not quite ready for your consumption. Do you work in Eclipse? Can I just zip my project directory or would you like something that runs straight from the command line?

/Daryl

Rob Bygrave

unread,
Jul 10, 2014, 8:54:42 AM7/10/14
to ebean@googlegroups
Yes I use eclipse. Zipping it up is good.

Thanks, Rob.


--

Daryl Stultz

unread,
Jul 15, 2014, 8:19:41 AM7/15/14
to eb...@googlegroups.com


On Monday, July 7, 2014 4:00:57 PM UTC-4, Daryl Stultz wrote:
Hi Rob,

I'm having a strange "class" of exceptions happening on a high percentage of unit tests involving Ebean 4.

It turns out my entities are extending a base class that is an @Entity while it needs to be a @MappedSuperclass. Rob is looking in to throwing a more meaningful error at the point of failure.

/Daryl 
Reply all
Reply to author
Forward
0 new messages