Given an abstract class with single table inheritance
@Entity
@DynamicUpdate
@Table(name="comments")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="commentable_type",
discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorOptions(force=true)
public abstract class Comment implements Serializable {
}
and some concrete implementations
@Entity
@DiscriminatorValue("News")
public static class BulletinComment extends Comment {
@JoinColumn(name="commentable_id", referencedColumnName="id")
@ManyToOne(fetch=FetchType.EAGER, optional=false)
private Bulletin commentable;
}
@Entity
@DiscriminatorValue("Page")
public static class PageComment extends Comment {
@JoinColumn(name="commentable_id", referencedColumnName="id")
@ManyToOne(fetch=FetchType.EAGER, optional=false)
private Page commentable;
}
and another entity that joins those comments polymorphic
@Entity
public class Activity {
@JoinColumn(name="reference_object_id", referencedColumnName="id")
@ManyToOne(fetch=FetchType.EAGER, optional=false)
private Comment object;
}
a query on activities fails with an entity not found exception. Hibernate 4.3.x inner joins the first commendable (From BulletinComment) in a query that ends in " where comment0_.id=32782 and comment0_.commentable_type in ('News', 'Page')" " but the given id is a PageComment.
The example works in Hibernate 4.2.x. It also works in Hibernate 4.3 if i remove the optional flag in the ManyToOne relations but to me, those associations are not optional and i see no reason to remove it.
And ideas or arguments why i should made them optional with hibernate 4.3? This actually not my only example.
Thanks for looking!
|