hibernate - JPA and "anonymous" classes in scala -


i'm bit stuck , don't understand what's going on. 1 doesn't work

@entity @dynamicinsert @dynamicupdate @selectbeforeupdate @table class entity {   @column(nullable = false)   var owner: string = _ }       val myentity = new entity() {             owner = "some owner 1"           }       session.persist(myentity) 

hibernate throws exception:

java.lang.illegalargumentexception: unknown entity:persistence.dao.entitydaotest$$anonfun$13$$anonfun$14$$anon$5     @ org.hibernate.internal.sessionimpl.firepersist(sessionimpl.java:777) 

this 1 works:

val myentity = new entity() entity.owner = "some owner 1" session.persist(myentity) 

why? why hibernate don't recognize entity instance?

upd: @sheinbergon, thanks, it's clear. forgot annotations lost. there possibility set entity fields shortcut? writing

val myentity = new myentity() myentity.owner = "some owner" myentity.someotherproperty = "value" 

is super boring

one more question 1 works:

val parent = new parent     parent.owner = "our parent"     parent.addchild(new child() {       name = "first parent's child"       addgrandchild(new grandchild() {         name = "grand child name"         addgrandgrandchild(new grandgrandchild() {           name = "grand grand child name"           address = new address() {             id = 1l           }         })       })     }) 

why? child, grandchild, grandgrandchild created anonymously. addchild, addgrandchild, addgrandgrandchild list mutators.

def addchild(child: child): unit = {     if (children == null) {       children = new util.arraylist[child]()     }     if (option(child.parent).isempty) {       child.parent =     }     children.add(child)   } 

what doing here instantiating class anonymously in scala , , well... creates anonymous implementation of class entity ( instantiating interface anonymously in java).

you can see printing class name - println(myentity.getclass) in both cases

annotations applied original class not apply anonymous 1 (reflection can still find them in super class, that's code scanning them) , guess that's why you're getting various jpa exceptions

in response added sub-questions

  • regarding shortcut - why don't use companion objects factories or turn class case class (with defaults), allowing nicer, more flexible initialization.
  • regarding second object graph(and assuming eachof classes annotated) - again depends on how reflective code treats objects scans. it's possible ( , more likely, given won't scan each member of collection annotations ) takes annotation definitions erased type ( possible it's fqdn class name parameterizedtype in java's reflection api) of collection , not actual members of collection , that's why works. i'm not sure field definitions though (they present in "super" class), there's no "magic" here, plain old reflection scans.

Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -