hibernate - HQL impossible to read attribute which is an instance of subclass -
first, want have read dozens of articles inheritance mapping or polymorphic fetch hibernate without finding solution problem. though case simple.
when executing code below: list meals = sessionfactory.getcurrentsession() .createquery("select m meal m").list(); error below: org.hibernate.wrongclassexception: object id: 1 not of specified subclass: com.mypack.fruit (discriminator: orange)
actually, can't read abstract class fruit subclassed in orange or apple
a meal contains of fruit can orange or apple
@entity @inheritance(strategy = inheritancetype.single_table) @table(name = "fruit") public abstract class fruit { @id @generatedvalue @column(name = "id", unique = true) private integer id; } @entity(name = "orange") //this discriminatorvalue stored in dtype column public class orange extends fruit { } @entity(name = "apple") //this discriminatorvalue stored in dtype column public class orange extends fruit { } @entity @table(name = "meal") public class meal { ... @manytoone @joincolumn(name = "fruit_id", nullable = false) private fruit fruit = null; }
the fetch not lazy shouldn't have problem proxy. don't understand why hibernate not able find out such fruit orange or apple fruit table contains in dtype column discriminator orange or apple
i grateful solve problem. (sorry vague english)
. .
after further research found @discriminatoroptions(force = true) solve trouble finally...helplessly. have learned if hibernate has information find concrete class, annotation @discriminatoroptions(force = true) needed in cases. anyway, tell hibernate : "hey hibernate don't try instanciate abstract fruit class, because abstract. hey hibernate try rely on entity @discriminatorvalue set on" .. hibernate deaf :(
replace , try
list meals = sessionfactory.getcurrentsession().createquery("from meal").list();
Comments
Post a Comment