JAVA EE - id 标记未在 orm.xml 中解析
Posted
技术标签:
【中文标题】JAVA EE - id 标记未在 orm.xml 中解析【英文标题】:JAVA EE - id tag are not parsed in orm.xml 【发布时间】:2018-01-26 23:00:59 【问题描述】:我是 JPA 领域的新手,我尽量不使用注释(仅使用 XML 文件 (orm.xml) ),所以我遵循本教程of mapping in JPA, 突然spring boot无法创建一个名为 entityManagerFactory 的bean,因为实体 Formation 没有标识符,但问题出在 orm.xml 文件中,我在 Formation 类中定义了与属性同名的标签ID ,this a link to project,这是项目的实体:
学生:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Etudiant implements Serializable
private Long id;
private String nom;
private String prenom;
private Formation formation;
形成:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Formation implements Serializable
//@Id @GeneratedValue
private Long id;
private String nom;
private int duree;
//@OneToMany(mappedBy="formation", fetch=FetchType.EAGER)
@JsonIgnore
private Collection<Etudiant> etudiants;
这是 orm.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings>
<entity class="org.sid.dao.entity.Formation">
<table name="formations">
<attributes>
<id name="id">
<genrated-value strategy="AUTO"></genrated-value>
</id>
<basic name="nom"></basic>
<basic name="duree"></basic>
<one-to-many mappedby="formation" fetch="lazy">
<basic name="etudiants"></basic>
</one-to-many>
</attributes>
</table>
</entity>
<entity class="org.sid.dao.entity.Etudiant">
<table name="etudiants">
<attributes>
<id name="id">
<genrated-value strategy="AUTO"></genrated-value>
</id>
<basic name="nom"></basic>
<basic name="prenom"></basic>
<many-to-one>
<Join-column name="id_formation">
<basic name="formation"></basic>
</Join-column>
</many-to-one>
</attributes>
</table>
</entity>
</entity-mappings>
这是个例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: org.sid.dao.entity.Formation
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.sid.ApplicationSyntheseApplication.main(ApplicationSyntheseApplication.java:22) [classes/:na]
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: org.sid.dao.entity.Formation
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:717) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 16 common frames omitted
问题是我确定 orm.xml 文件已在构建中加载。 :
【问题讨论】:
【参考方案1】:我使用orm.xml
时遇到了同样的异常。为 id
字段添加 getter 和 setter 解决了这个问题。
public Long getId()
return id;
public void setId(Long id)
this.id = id;
【讨论】:
【参考方案2】:您的orm.xml
无效。 attributes
标签应该在table
标签之外。我建议将以下标题添加到您的orm.xml
。
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
然后在一个不错的 IDE 中打开您的 orm.xml
(IntelliJ 或 Eclipse 应该可以解决问题)并修复您的 orm.xml
中的所有验证错误。
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<entity class="org.sid.dao.entity.Formation" access="FIELD">
<table name="formations" />
<attributes>
<id name="id">
<generated-value strategy="AUTO" />
</id>
<basic name="nom" />
<basic name="duree" />
<one-to-many name="etudiants" mapped-by="formation" fetch="LAZY" />
</attributes>
</entity>
<entity class="org.sid.dao.entity.Etudiant" access="FIELD">
<table name="etudiants" />
<attributes>
<id name="id">
<generated-value strategy="AUTO" />
</id>
<basic name="nom"/>
<basic name="prenom"/>
<many-to-one name="formation">
<join-column name="id_formation" />
</many-to-one>
</attributes>
</entity>
</entity-mappings>
它应该看起来像这样。
【讨论】:
【参考方案3】:-
看起来您的 xml 中有拼写错误 —
genrated-value
而不是 generated-value
要让 spring boot 使用 orm.xml 文件,您应该说 EntityManagerFactory 来使用它。即,如果您的 JPA 提供程序是 Hibernate:
@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration
@Bean
@Override
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder factoryBuilder)
final LocalContainerEntityManagerFactoryBean ret = super.entityManagerFactory(factoryBuilder);
ret.setMappingResources("orm.xml");
return ret;
【讨论】:
感谢您的回复,但我也有同样的错误,现在您能告诉我任何可以包含在项目中以使用配置类的依赖项吗?还有另一种方法可以像另一个 XML 文件一样吗?? 你的 orm.xml 在哪里? @ekka 看起来应该在 src/main/resources/META-INF 是的,很抱歉它在 /src/main/resouces/META-INF 中:现在我收到一个新错误:无法解析显式命名的映射文件:orm.xml:起源(orm.xml) 如果orm.xml
位于默认位置,则无需使用setMappingResources
。另外建议扩展自动配置是您不应该做的事情。以上是关于JAVA EE - id 标记未在 orm.xml 中解析的主要内容,如果未能解决你的问题,请参考以下文章