Mapping subclass list in Java using hibernate annotations -
i have problem mapping list of subclasses:
model situation - have abstract class:
@entity @inheritance(strategy=inheritancetype.single_table) @discriminatorcolumn( name="shapetype", discriminatortype=discriminatortype.string ) public abstract class shape{ @id @generatedvalue(strategy=generationtype.identity) protected long id; @column(name="owner_id") private long ownerid; @manytoone @joincolumn(updatable=false, insertable=false, name="owner_id") private owner owner; }
and subclasses:
@entity @discriminatorvalue(value="triangel") public class triangel extends shape { }
and:
@entity @discriminatorvalue(value="circle") public class circle extends shape { }
then, have class owner
, has list of subclasses:
@entity public class owner { @id @generatedvalue(strategy=generationtype.identity) private long id; @onetomany(fetch = fetchtype.lazy, mappedby = "owner", targetentity=shape.class) private list<triangel> triangels; @onetomany(fetch = fetchtype.lazy, mappedby = "owner", targetentity=shape.class) private list<circle> circles; }
when loop on triangel
list:
for(object triangel: owner.gettriangels()){ //using triangel type throws classcastexception logger.info(triangel.tostring()); }
it iterates shapes objects, not triangel
objects. seems me, hibernate ignores discriminatorcolumn
during selection subclasses in situation.
mind, without specification targetentity
shape.class
in @onetomany
, application did not start , had problem mapping initialization.
configuration pom.xml:
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.34</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.3.7.final</version> </dependency> <dependency> <groupid>org.hibernate.javax.persistence</groupid> <artifactid>hibernate-jpa-2.1-api</artifactid> <version>1.0.0.final</version> </dependency> <dependency> <groupid>org.eclipse.persistence</groupid> <artifactid>javax.persistence</artifactid> <version>2.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.2.2</version> <scope>provided</scope> </dependency>
how supposed correct mapping configuration of design?
that's got fixed in hibernate 5.2.7: https://hibernate.atlassian.net/browse/hhh-11375
Comments
Post a Comment