org.hibernate.AnnotationException:没有为使用JPA XML实体映射的实体指定标识符

时间:2016-11-03 11:06:19

标签: java hibernate jpa

我现在正在使用Hibernate 5.2.4.Final(从5.2.3升级,看看这是否能解决错误,但仍然没有成功)。

我有以下SQL:

 CREATE TABLE Token (
     tokenId        BIGINT PRIMARY KEY
    ,uuid           UUID
    ,state          INTEGER
    ,creationDate   TIMESTAMP
    ,expirationDate TIMESTAMP
    ,userId         BIGINT REFERENCES MyUser(id)
);
CREATE SEQUENCE TokenIdSeq;

以及映射文件中的以下实体:

<entity name="Token" class="com.library_provider.Token" access="FIELD">
    <id name="tokenId">
        <generated-value strategy="SEQUENCE" generator="TokenIdSeq" />
        <sequence-generator name="TokenIdSeq" sequence-name="TokenIdSeq" allocation-size="1" />
    </id>
    <basic name="uuid" />
    <basic name="state" />
    <basic name="creationDate" />
    <basic name="expirationDate" />
    <many-to-one name="userData" class="com.company.MyUser" column="userId" />
</entity>

Token类是:

public class Token implements Serializable {

    private static final long serialVersionUID = 12432342342423400L;

    private Long tokenId;

    private UUID uuid;

    private Integer state;

    private Date creationDate;

    private Date expirationDate;

    public UserData userData;
//....
}

启动Tomcat时出现以下异常:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.library_provider.Token
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:231)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:719)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:846)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:873)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:151)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 21 more

我正在使用JPA XML映射文件而不是注释。为什么抛出这个异常?我的其他实体工作正常,区别在于我在这些字段中使用<id name="id">,但在这种情况下,我无法决定/更改tokenId字段的名称。

1 个答案:

答案 0 :(得分:0)

JPA映射文件缺少<attributes>内的<entity>标记。

应该是:

<entity name="Token" class="com.library_provider.Token" access="FIELD">
    <attributes>
        <id name="tokenId">
            <generated-value strategy="SEQUENCE" generator="TokenIdSeq" />
            <sequence-generator name="TokenIdSeq" sequence-name="TokenIdSeq" allocation-size="1" />
        </id>
        <basic name="uuid" />
        <basic name="state" />
        <basic name="creationDate" />
        <basic name="expirationDate" />
        <many-to-one name="userData" class="com.company.MyUser" column="userId" />
    </attributes>