我是JPA世界的新手,我尝试不使用注释(只使用XML文件(orm.xml)),所以我按照本教程of mapping in JPA进行操作, 突然spring boot无法创建一个名为entityManagerFactory的bean,因为实体Formation没有标识符,但问题出在orm.xml文件中,我定义了同名的tag id形成类this a link to project中的属性,这是项目的实体:
Etudiant:
@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文件已经在构建中加载。 :
答案 0 :(得分:0)
我在使用orm.xml
时遇到了相同的例外。在id
字段中添加吸气剂和吸气剂解决了这个问题。
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
答案 1 :(得分:0)
您的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>
它应该看起来像这样。
答案 2 :(得分:-1)
genrated-value
而不是generated-value
对于使用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;
}
}