Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.2.0-CR1
Hibernate annotations version: 3.1.0-beta10
Name and version of the database you are using : mysql Ver 12.22 Distrib 4.0.24
Hello, I'm trying to map One To many bidirectional associations, I explain :
I have the following class :
Code:
@Entity
public Class CV
{
private Long id;
private List<IFunctionalSkill> functionalSkills = new ArrayList<IFunctionalSkill>();
private List<ITechnicalSkill> technicalSkills = new ArrayList<ITechnicalSkill>();
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* @return Returns the functionalSkills.
*/
@OneToMany(mappedBy="cv", targetEntity=FunctionalSkill.class, fetch = FetchType.EAGER)
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<IFunctionalSkill> getFunctionalSkills()
{
return functionalSkills;
}
/**
* @return Returns the technicalSkills.
*/
@OneToMany(mappedBy="cv", targetEntity=TechnicalSkill.class, fetch = FetchType.EAGER)
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<ITechnicalSkill> getTechnicalSkills()
{
return technicalSkills;
}
}
CV has two bidirectional One To Many associations with technicalSkill and FunctionalSkill.
TechnicalSkill and FunctionalSkill are inherited from the abstract class "Skill".
I use the SINGLE_TABLE strategy :
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TypeSkill", discriminatorType=DiscriminatorType.STRING)
public abstract class Skill implements ISkill
{
private Long id;
private ICV cv;
private Map<String, ISkillDetail> details = new LinkedHashMap<String, ISkillDetail>();
public Skill() {}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne(targetEntity=CV.class)
public ICV getCv() {
return cv;
}
public void setCv(ICV cv) {
this.cv = cv;
}
// other methods ..etc
/**
* @return Returns the details.
*/
@OneToMany(mappedBy="skill", targetEntity=SkillDetail.class, cascade = CascadeType.ALL)
@MapKey(name="language")
public Map<String, ISkillDetail> getDetails()
{
return details;
}
/**
* @param details The details to set.
*/
public void setDetails(Map<String, ISkillDetail> details)
{
this.details = details;
}
...
}
Code:
@Entity
@DiscriminatorValue("FunctionalSkill")
public class FunctionalSkill extends Skill implements IFunctionalSkill
{
private IFunctionalSkillType type = null;
public FunctionalSkill() {}
..etc
}
Code:
@Entity
@DiscriminatorValue("TechnicalSkill")
public class TechnicalSkill extends Skill implements ITechnicalSkill
{
private ITechnicalSkillType type = null;
public TechnicalSkill() {}
..etc
}
In my web application, when I create a CV (ID is 1 so) , eachtime I add a functionalSkill or a technicalSkill ,the SAVING opération is OK :
In my database I have the table CV and the table SKILL which has a FK "cv_id" . The discriminator column is either "functionalSkill" or "technicalSkill". the cv_id column is not null and has the value 1.
But each time I want to retrieved (fetchmode = eager for the needs of the application) my CV with the ID = 1, the getFunctionalSkill() or the getTechnicalSkill() methods are called.
And ... I have the following exception :
Quote:
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 1 was not of the specified subclass: fr.model.hibernate.FunctionalSkill (loaded object was of wrong class); nested exception is org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: fr.model.hibernate.FunctionalSkill (loaded object was of wrong class)
org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: fr.model.hibernate.FunctionalSkill (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:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1679)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:755)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2821)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
Error of mapping of inheritance somewhere??!
Please, I really don't know how to solve my problem since one week !!! :( :(
Any help will be appreciate ![/b]