Object 'cannot be cast' using Hibernate in Play for Scala -


i'm running hibernate 5.x in play scala 2.5. hibernate accesses sap hana database.

problem happens when touch code , play compiles automatically. right after code compiles, when run application, if code invokes hibernate function exception below admin.dates.datehib cannot cast admin.dates.datehib datehib hibernate annotated class. note don't change neither hibernate objects nor functions use hibernate objects. still, when run code after editing cannot cast error.

the workaround restart play, cannot restart play everytime touch code.

i'm not sure has problem, i'm running in same application slick 3.1 accessing mysql.

any ideas?

this code fails:

 def findlastdayholiday (month: int, year: int) = {      val session = hibernateutil.sessionfactorybank.opensession      try {          val query = session.createquery("from datehib month=:month , year=:year")          query.setmaxresults(1)          query.setparameter("year", year)          query.setparameter("month", month)          val list = query.list.asscala.tolist.map(_.asinstanceof[datehib])          if (list.length>0)              some(list(0))          else               none       }       catch {         case e:exception => throw new exception ("failure: " + e.getmessage)       }       session.close    } 

and exception:

play.api.http.httperrorhandlerexceptions$$anon$1: execution exception[[exception: failure in findlastdayholiday: admin.dates.datehib cannot cast admin.dates.datehib]]         @ play.api.http.httperrorhandlerexceptions$.throwabletousefulexception(httperrorhandler.scala:293)         @ play.api.http.defaulthttperrorhandler.onservererror(httperrorhandler.scala:220)         @ play.api.globalsettings$class.onerror(globalsettings.scala:160)         @ play.api.defaultglobal$.onerror(globalsettings.scala:188)         @ play.api.http.globalsettingshttperrorhandler.onservererror(httperrorhandler.scala:100)         @ play.core.server.netty.playrequesthandler$$anonfun$2$$anonfun$apply$1.applyorelse(playrequesthandler.scala:100)         @ play.core.server.netty.playrequesthandler$$anonfun$2$$anonfun$apply$1.applyorelse(playrequesthandler.scala:99)         @ scala.concurrent.future$$anonfun$recoverwith$1.apply(future.scala:344)         @ scala.concurrent.future$$anonfun$recoverwith$1.apply(future.scala:343)         @ scala.concurrent.impl.callbackrunnable.run(promise.scala:32) caused by: java.lang.exception: failure in findlastdayholiday: admin.dates.datehib cannot cast admin.dates.datehib         @ admin.dates.dateobjdao.findlastdayholiday(dateobjdao.scala:126)         @ ds.formula.process.runformula.getlastdayholiday(runformula.scala:665)         @ ds.formula.process.runformula.getfromtodates(runformula.scala:610)         @ ds.formula.process.runformula.run(runformula.scala:145)         @ ds.formula.process.runformula.dotest(runformula.scala:69)         @ ds.formula.process.runformula$$anonfun$test$1.apply(runformula.scala:61)         @ ds.formula.process.runformula$$anonfun$test$1.apply(runformula.scala:59)         @ login.authentication$loggedaction$$anonfun$invokeblock$1.apply(loggedaction.scala:39)         @ login.authentication$loggedaction$$anonfun$invokeblock$1.apply(loggedaction.scala:34)         @ scala.concurrent.future$$anonfun$flatmap$1.apply(future.scala:251) 

i think cchantep right. every time play reloads application uses different classloader.

i see 2 things might happening:

  1. you using few singletons loaded using classloader create play, , when play reloads singletons not being destroyed, leading kind of problem see.
  2. you use few services should shutdown , restart when play reloads, don't, leads same problem point 1 above.

but first, let's make sure above correct, see how can diagnose more precisely: add log entries in failing code print classloader used in different objects.

     val list = query.list.asscala.tolist.map { e =>         log.debug("class loaded with: " + e.getclass.getclassloader)        log.debug("current class loader: " + classof[datehib].getclassloader)        e.asinstanceof[datehib]      } 

my guess classloader printed same when first start application, after play reloads, first classloader printed doesn't change, second 1 new instance... give try.

as fixing issues, should play's lifecycle hooks: https://www.playframework.com/documentation/2.6.x/scaladependencyinjection#stopping/cleaning-up


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -