java - org.hibernate.TransientObjectException when all @OneToMany are cascade=ALL -


i have small data structure data structure serialize in hibernate using jpa annotations:

(below simplified)

public class result {    @id    @generatedvalue(strategy=generationtype.identity)    public int id;      @onetomany(cascade=cascadetype.all)    @ordercolumn("row")    @joincolumn("resultid)    public list<row> rows }  public class row {    @id    @generatedvalue(strategy=generationtype.identity)    public int id;     @elementcollection    @ordercolumn("col")    public list<double> value; } 

when try persist() result, transientobjectexception. how can be? shouldn't cascade=all take care of this?

turns out, i've run quite interaction between hibernate , guava (google collections). in fact, 1 feature of guava needs more explicitly announced.

the code sample in question incomplete. relevant (and missing) detail fact had constructor of result took list<list<double>> parameter - being more natural rest of app.

unfortunately, hibernate not @ storing two-dimensional arrays, row class quick workaround.

to maintain interface rest of code, convert list<list<>> list<row> in constructor using - else - lists.transform guava.

when tried save resulting objects db, got exception hibernate:

org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: <my class here> 

i debugged bit deeper, , realized row object had setid called on not row object getting getid called (i had getters , setters - simplification in code sample).

then dawned on me. transform() produces view of underlying list, not copy. every time ask transformed object, it's created scratch, brand new "id" field initialized 0.

let lesson: if transform adds possibility of state, follow suggestion in guava's lists.transform docs:

to avoid lazy evaluation when returned list doesn't need view, copy returned list new list of choosing.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -