具有枚举类型的单表继承

时间:2012-11-02 12:08:50

标签: hibernate

我有如下的父子实体关系。在这里,我将为单个表映射此继承,即User

@Entity
@Table(name = "USER")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "USER_TYPE", discriminatorType = DiscriminatorType.STRING,    length = 20)
@DiscriminatorValue("USER")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    .
    .
    @Enumerated(EnumType.STRING)
    @Column(name = "USER_TYPE", insertable = false, updatable = false)
    private UserType userType;
    .
    .

    //setters, getters

}


@Entity
@DiscriminatorValue("MANAGER")
public class Manager extends User implements Serializable {
    // manager specific fields
}

User Types :

public enum UserType {
    MANAGER,
    ADMIN,
    ACCOUNTS_CLEARK
}

当我持久保存User对象时,用户类型列只获得USER,甚至我在persistance对象中设置用户类型(即UserType.MANAGER)。实际的hibernate插入语句如下所示;

Hibernate: insert into USER (ID, DOB, EMAIL, FIRST_NAME, MIDDLE_NAME, LAST_NAME,     NICKNAME, USER_NAME, PASSWORD, USER_TYPE) values (null, ?, ?, ?, ?, ?, ?, ?, ?, 'USER')

当我尝试从用户表中检索所有记录时,我遇到异常;

java.lang.IllegalArgumentException: Unknown name value for enum class     com.test.domain.user.UserType: USER
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:127)
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:132)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
    at     org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2267)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1443)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1371)
    at org.hibernate.loader.Loader.getRow(Loader.java:1271)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:619)
    at org.hibernate.loader.Loader.doQuery(Loader.java:745)
    at     org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
    at org.hibernate.loader.Loader.list(Loader.java:2187)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at     org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at     org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: No enum const class com.test.domain.user.UserType.USER
    at java.lang.Enum.valueOf(Enum.java:196)
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:124)
    ... 47 more

我想每次我要保存用户时,它会选择@DiscriminatorValue,这是USER并保存在用户类型字段中。任何人都会遇到这种情况并采取相应措施。请注意,用户类型表中没有USER类型。

1 个答案:

答案 0 :(得分:1)

属性userType是注释中指定的readonly(insert = false,u8pdate = false),因此在插入时永远不会考虑它。唯一考虑的值是为类指定的鉴别器值,并且对于每个持久保存的User对象设置为USER,并且每个Manager对象的MANAGER都保持不变。因此,修复Discriminator值与UserType枚举

之间的不匹配