scala - Unable to access Seq elements and return entire Seq as Json -
so extremely new scala , seem doing horribly wrong simple logic. need query database websitetemplates. trying access first element of sequence (simply practice) keep getting classcastexception - [ljava.lang.object; cannot cast models.websitetemplate. next, want return entire seq obtained json. tried json.arr() won't work. searched none matches exact use case strangely. here relevant code -
websitecontroller
@singleton class websitetemplatecontroller @inject()(websitetemplatedao: websitetemplatedao, db: db) extends controller{ def index = action.async { implicit request => db.withtransaction() { implicit em => { try { val templates: seq[websitetemplate] = websitetemplatedao.findall() val template = templates(0) logger.info("the size " + template.name) future(ok(json.obj( "message" -> "success" ))) } catch { case e: exception => e.printstacktrace() future(internalservererror(s"lag gaye")) } } } } }
websitedaoimpl
@implementedby(classof[websitetemplatedaoimpl]) trait websitetemplatedao extends dao[websitetemplate]{ def findall()(implicit em: entitymanager): seq[websitetemplate] } @singleton class websitetemplatedaoimpl @inject()(db: db) extends daoimpl(classof[websitetemplate]) websitetemplatedao{ override def findall()(implicit em: entitymanager): seq[websitetemplate] = { val query = em.createquery(s"select idint, name websitetemplate") db.executequery(query) } }
daoimpl
trait dao[t] { def findbyid(id:string)(implicit em:entitymanager) : option[t] def create : t } class daoimpl[t](cls:class[t]) extends dao[t] { def findbyid(id: string)(implicit em: entitymanager): option[t] = { val query = em.createquery(s"select c ${cls.getname} c c.id=:id") query.setparameter("id",id) val list = query.getresultlist if (list.nonempty) { option(list(0).asinstanceof[t]) } else { none } } override def create: t = { cls.newinstance() } }
websitetemplate
@entity @table(name = "templates") @entitylisteners(array(classof[setforeignkeyonsave])) class websitetemplate{ @id @column(name = "id") @beanproperty var intid: java.lang.integer = _ /*@transient override var id: string = _*/ @beanproperty @column(name = "name") var name: string = _ }
a bit more explanation of problem. on getting type of templates(0) using getclass()
method, shows [ljava.lang.object
whereas expect of type websitetemplate
.
Comments
Post a Comment