无法在Hibernate映射枚举映射中实例化自定义类型错误

时间:2012-12-23 12:08:59

标签: hibernate enums hibernate-mapping

我尝试将POJO类映射到mysql数据库表。该表尚不存在。我使用以下映射文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="sor.hibernate.actors.User" table="testuser" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="30" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="128" />
        </property>
        <property name='userLevel'>
            <type name="org.hibernate.type.EnumType">
                <param name="UserLevel">sor.hibernate.acotrs.UserLevel</param>
            </type>
        </property>
        <property name="registrationDate" type="java.lang.Date">
            <column name="regDate" length="10" />
        </property>
        <property name="birthday" type="java.lang.Date">
            <column name="birthday" length="10" />
        </property>
    </class>
</hibernate-mapping>

User类如下(我删除了访问者方法,但它们存在于代码中!):

public class User {

    private long id;
    private String username;
    private String password;
    private UserLevel userLevel;
    private Date birthday;
    private Date registrationDate;

}

枚举类如下:

public enum UserLevel {

    GUEST,
    SUSPENDED,
    SUBSCRIBER,
    CLERK;

    private UserLevel() {
        // TODO Auto-generated constructor stub
    }
}

我遇到的错误是:

23-dic-2012 12.56.24 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
23-dic-2012 12.56.24 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
23-dic-2012 12.56.24 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
23-dic-2012 12.56.24 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
23-dic-2012 12.56.24 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
23-dic-2012 12.56.24 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/actors/User.hbm.xml
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Unable to instantiate custom type: org.hibernate.type.EnumType
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:193)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:179)
    at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:103)
    at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:130)
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:296)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:289)
    at org.hibernate.mapping.Property.isValid(Property.java:238)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1736)
    at sor.hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:34)
    at tests.FirstTest.main(FirstTest.java:22)
Caused by: java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:170)
    at org.hibernate.type.EnumType.setParameterValues(EnumType.java:173)
    at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:131)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:189)
    ... 12 more
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/actors/User.hbm.xml
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: sor.hibernate.actors.User -> sor.hibernate.actors.User
23-dic-2012 12.56.25 org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: sor.hibernate.actors.User -> User
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource sor/hibernate/actors/User.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3409)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3398)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3386)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1731)
    at sor.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:73)
    at sor.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:55)
    at tests.FirstTest.main(FirstTest.java:22)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping sor.hibernate.actors.User
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2574)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:172)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3406)
    ... 7 more
Exception in thread "main" java.lang.NullPointerException
    at tests.FirstTest.main(FirstTest.java:23)
程序

Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();

User  u = new User();
u.setUsername("first");
u.setPassword("ine");
u.setUserLevel(UserLevel.CLERK);

tx.commit();
session.close();

添加hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">qwerty</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">testDBdriverMySql</property>
        <property name="javax.persistence.validation.mode">none</property>
        <mapping resource="sor/hibernate/actors/User.hbm.xml" />
    </session-factory>

</hibernate-configuration>

1 个答案:

答案 0 :(得分:2)

我发现了问题。我应该在下一节中更改hbm文件

    <property name="userLevel">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">sor.hibernate.actors.UserLevel</param>
            <param name="type">12</param>
        </type>
    </property>

这里已经解释过:

http://www.thetekblog.com/2011/06/hibernate-3-enums-in-the-hbm-file-not-annotations/

我注意到还有一个错字。 acotrs - &gt;演员