JAVA EE-id标记未在orm.xml中解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA EE-id标记未在orm.xml中解析相关的知识,希望对你有一定的参考价值。

我是JPA世界的新手,我尝试不使用注释,(只用XML文件(orm.xml)),所以我按照本教程of mapping in JPA,突然spring boot无法创建一个名字的bean它是entityManagerFactory,因为实体Formation没有标识符,但是问题出在orm.xml文件中,我在Formation class,this a link to project中定义了具有相同属性名称的标记id,这是项目的实体:

学生:

@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文件已经在构建中加载。 :

答案

我使用orm.xml有同样的例外。为id字段添加getter和setter解决了这个问题。

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
另一答案

你的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>

看起来应该是这样的。

另一答案
  1. 看起来你的xml中有拼写错误 - genrated-value而不是generated-value
  2. 对于使用orm.xml文件的spring boot,你应该说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; } }

以上是关于JAVA EE-id标记未在orm.xml中解析的主要内容,如果未能解决你的问题,请参考以下文章

深入解析Java垃圾回收机制

JAR (Java App) 中的标记文件 (.tag) 未在 RAD 7 中获取

JAR 中的资源未在 IntelliJ 中正确解析

谷歌地图标记未在 Chrome 中显示

67.Java垃圾收集机制对象引用垃圾对象的判定垃圾收集算法标记—清除算法标记—整理算法分代收集垃圾收集器性能调优

日期对象 SimpleDateFormat 未在 Java(Android)环境中正确解析时间戳字符串