p.chevillon, I can confirm that your suggestion solves this problem. I found this thread by searching for the term "WrongClassException".
I imagine you have probably solved your problem by now, but in case not then let me show you how the @Where annotation fixed the issue in my case.
I'm afraid I'm not familiar with your technique of mapping methods rather than properties, so it's easier for me to show you from my class design.
This is my top level class
Code:
@Entity
@Table(name="FIELD_CATEGORY")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "FIELD_TYPE_ID", discriminatorType = DiscriminatorType.INTEGER)
public abstract class FieldCategory {
// content not important
}
I have two concrete subclasses:
Code:
@Entity
@DiscriminatorValue("1")
public class RoleFieldCategory extends FieldCategory{
@ManyToOne
@JoinColumn(name = "CLIENT_ID", nullable = false)
private Client client;
// rest not important
}
@Entity
@DiscriminatorValue("2")
public class EmployeeFieldCategory extends FieldCategory{
@ManyToOne
@JoinColumn(name = "CLIENT_ID", nullable = false)
private Client client;
// rest not important
}
Then, in the Client class, I have a collection of EmployeeFieldCategory(s) and a collection of RoleFieldCategory(s)
Code:
@OneToMany(mappedBy="client",fetch=FetchType.LAZY,targetEntity=RoleFieldCategory.class)
@Fetch(FetchMode.SUBSELECT)
@Where(clause="FIELD_TYPE_ID=1")
private List<RoleFieldCategory> roleFieldCategories;
@OneToMany(mappedBy="client",fetch=FetchType.LAZY,targetEntity=EmployeeFieldCategory.class)
@Fetch(FetchMode.SUBSELECT)
@Where(clause="FIELD_TYPE_ID=2")
private List<EmployeeFieldCategory> employeeFieldCategories;
I theory Hibernate has enough information from the @OneToMany annotation to build the correct queries for these mappings, but without the @Where annotation I get a WrongClassExeption when I try to access either collection. I can only assume this is a bug in hibernate, though at least there is a workaround.
Notice that both concrete subclasses of FieldCategory have an instance of Client. The logical class design would be to have the Client property in the abstract FieldCategory class. However, hibernate will not accept @OneToMany(mappedBy="an inherited property") so I'm forced to have abstract getters and setters for a Client property in FieldCategory and reproduce the property in each and every concrete subclass. Again this is a flaw in hibernate as far as I'm concerned, but again at least there's a workaround.
I hope this helps you or someone else who happens to find this thread.