Need help with Hibernate?
Code:
@Table(name="PERMISSION")
@DiscriminatorColumn(name="TYPE",...)
public abstract class Permission {..}
@DiscriminatorValue("SERVERSIDE")
class ServerSidePermission extends Permission {...}
@DiscriminatorValue("CLIENTSIDE")
class ClientSidePermission extends Permission {...}
<< class Role >> has these mapping definitions:
Code:
@ManyToMany(targetEntity = ServerSidePermission.class, fetch = FetchType.EAGER)
@JoinTable(name = "ROLE_PERMISSION", joinColumns = {
@JoinColumn(name="ROLE_ID")
}, inverseJoinColumns = {
@JoinColumn(name="PERMISSION_ID")
})
private Set<ServerSidePermission> serverSidePermissions;
@ManyToMany(targetEntity = ClientSidePermission.class, fetch = FetchType.EAGER)
@JoinTable(name = "ROLE_PERMISSION", joinColumns = {
@JoinColumn(name="ROLE_ID")
}, inverseJoinColumns = {
@JoinColumn(name="PERMISSION_ID")
})
private Set<ClientSidePermission> clientSidePermissions;
When I try to load Role bean, this exception is thrown:
Code:
org.hibernate.WrongClassException: Object with id: 3 was not of the specified subclass: ServerSidePermission (loaded object was of wrong class)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1235)
at org.hibernate.loader.Loader.getRow(Loader.java:1186)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
The generated SQLs are
select serverside0_.ROLE_ID as ROLE1_1_, serverside0_.PERMISSION_ID as PERMISSION2_1_, serverside1_.ID as ID58_0_, serverside1_.NAME as NAME58_0_, serverside1_.DESCRIPTION as DESCRIPT4_58_0_ from CORPORATE.U_ROLE_PERMISSION serverside0_ left outer join CORPORATE.U_PERMISSION serverside1_ on serverside0_.PERMISSION_ID=serverside1_.ID where serverside0_.ROLE_ID=?
select clientside0_.ROLE_ID as ROLE1_1_, clientside0_.PERMISSION_ID as PERMISSION2_1_, clientside1_.ID as ID58_0_, clientside1_.NAME as NAME58_0_, clientside1_.DESCRIPTION as DESCRIPT4_58_0_ from CORPORATE.U_ROLE_PERMISSION clientside0_ left outer join CORPORATE.U_PERMISSION clientside1_ on clientside0_.PERMISSION_ID=clientside1_.ID where clientside0_.ROLE_ID=?
But there is no condition like clientside1_.TYPE='SERVERSIDE' in where clause. I think It should be cause of the discriminator definitions in subclasses.