org.hibernate.AnnotationException:没有为使用 JPA XML 实体映射的实体指定标识符
Posted
技术标签:
【中文标题】org.hibernate.AnnotationException:没有为使用 JPA XML 实体映射的实体指定标识符【英文标题】:org.hibernate.AnnotationException: No identifier specified for entity using JPA XML entity-mapping 【发布时间】:2016-11-03 11:06:19 【问题描述】:我现在正在使用 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
字段的名称。
【问题讨论】:
你的 Token 类有所有的 setter 和 getter 吗? 是的,确实如此...我只是在仔细查看后才发现问题,现在发布答案(感觉很愚蠢:D) 【参考方案1】:JPA 映射文件在<entity>
内缺少<attributes>
标记。
应该是:
<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>
【讨论】:
以上是关于org.hibernate.AnnotationException:没有为使用 JPA XML 实体映射的实体指定标识符的主要内容,如果未能解决你的问题,请参考以下文章