从hbm到JPA注释(子类)

时间:2013-11-06 12:35:13

标签: java hibernate hibernate-mapping hibernate-annotations

我正在将我的代码更新为JPA,并且在使用鉴别器

时出现错误
@Entity
@DiscriminatorValue("3")
public class WidgetContainer extends Square {
   ...
}

Square.java

@Entity
@Table(name = "square")
@DiscriminatorColumn(name = "squareType", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Square implements Indexable, Serializable{
   ...
}

我收到此错误

org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: sym.domain.Square (Discriminator: 3)

2 个答案:

答案 0 :(得分:1)

我有一个类似的问题,通过添加这个注释来解决:

@DiscriminatorOptions(force=true)

到继承结构的根(在你的情况下为Square)。我不知道是否有纯粹的JPA方法来实现这一点。

Java API for DiscriminatorOptions

答案 1 :(得分:0)

愚蠢的事情发生了...我想念这个子类加入hibernate配置

<bean id="sessionFactory"
        name="sessionFactoriBla"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="annotatedClasses">
            <list>
                                .......
                <value>com.domain.WidgetContainer</value>
                                ......
            </list>
        </property>
...

抱歉