Hello!
I use Hibernate 3.6.3. I have a class hierarchy, mapped by SINGLE_TABLE strategy.
Head annotations for basic class is:
Code:
@Entity
@Table(name = "element_field")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@AttributeOverride(name = "type",
column = @Column(name = "type", nullable = false, insertable = false, updatable = false))
@SequenceGenerator(name = "ELEMENT_FIELD_SEQ", sequenceName = "element_field_id_seq", allocationSize = 1)
public abstract class ElementField extends DomainEntity {
...
}
and child classes in hierarchy mapped as below:
Code:
@Entity
@DiscriminatorValue("string")
public class StringElementField extends ElementField {
...
}
and i have other class Task, which uses a collection of ElementField.
Code:
@OneToMany
@JoinColumn(name = "element_id", nullable = false)
@LazyCollection(LazyCollectionOption.TRUE)
public List<ElementField> getFields() {
return fields;
}
When i save Task object with collection of elements, hibernate throw exception:
Code:
Caused by: org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: su.ivt.sc.core.business.domain.element.field.ElementField
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:101)
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:123)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:603)
at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3911)
at org.hibernate.type.EntityType.replace(EntityType.java:285)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:508)
at org.hibernate.type.CollectionType.replace(CollectionType.java:575)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:563)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:491)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:255)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:867)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:851)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:855)
at su.ivt.sc.core.business.dao.impl.base.AbstractDao.save(AbstractDao.java:67)
at su.ivt.sc.core.business.services.task.TaskService.save(TaskService.java:884)
When i rewrite basic and child classes with @MappedSuperclass annotation as:
Code:
//For basic class
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@AttributeOverride(name = "type",
column = @Column(name = "type", nullable = false, insertable = false, updatable = false))
//For childs
@Entity
@Table(name = "element_field")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@DiscriminatorValue("string")
@SequenceGenerator(name = "ELEMENT_FIELD_SEQ", sequenceName = "element_field_id_seq", allocationSize = 1)
on startup application hibernate throw "org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class" exception.
Thank for help.