org.hibernate.AnnotationException:未使用JPA XML实体映射为实体指定标识符 [英] org.hibernate.AnnotationException: No identifier specified for entity using JPA XML entity-mapping
问题描述
我现在正在使用Hibernate 5.2.4.Final(已从5.2.3升级,以查看是否可以解决错误,但仍然没有成功).
I am now using Hibernate 5.2.4.Final (upgraded from 5.2.3 to see if this would fix the error, but still no success).
我有以下SQL:
CREATE TABLE Token (
tokenId BIGINT PRIMARY KEY
,uuid UUID
,state INTEGER
,creationDate TIMESTAMP
,expirationDate TIMESTAMP
,userId BIGINT REFERENCES MyUser(id)
);
CREATE SEQUENCE TokenIdSeq;
以及映射文件中的以下实体:
and the following entity in the mapping file:
<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时出现以下异常:
I am getting the following exception upon booting 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
字段的名称.
I am using the JPA XML mapping file instead of annotations. Why is this exception being thrown? My other entities are working fine, with the difference that I am using <id name="id">
in these fields, but in this case, I cannot decide/change the name of the tokenId
field.
推荐答案
JPA映射文件缺少<entity>
内的<attributes>
标记.
The JPA mapping file is missing the <attributes>
tag, inside the <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>
这篇关于org.hibernate.AnnotationException:未使用JPA XML实体映射为实体指定标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!