创建 entityManagerFactory 时出错。无法构建 Hibernate SessionF.SchemaManagementException:导出标识符 [new_user] 遇到多次

Posted

技术标签:

【中文标题】创建 entityManagerFactory 时出错。无法构建 Hibernate SessionF.SchemaManagementException:导出标识符 [new_user] 遇到多次【英文标题】:Error creating entityManagerFactory.Unable building Hibernate SessionF.SchemaManagementException:Export identifier[new_user]encountered more than once 【发布时间】:2022-01-14 04:35:11 【问题描述】:

我的控制器类 -

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.erecruitment.app.repository.InterviewerDetail;
import com.erecruitment.app.repository.usernameExists;

@RestController
@CrossOrigin(origins = "*")
public class ClsInterviewerDetails 
    @Autowired
    InterviewerDetail intDet;
    @CrossOrigin(origins = "*")
    @GetMapping("/interviewerDetail")
    public int interviewerDetail() 
        return intDet.interviewerDet();
    

我的 JpaRepository 扩展接口 -

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.erecruitment.app.model.ClsNew_User;
import com.erecruitment.app.model.user;
@Repository
public interface InterviewerDetail extends JpaRepository<ClsNew_User, String>      
    @Transactional
    @Modifying
    @Query(value="SELECT 
    users_id,first_name,last_name,role,select_technical_skill,additional_skills FROM new_user 
    WHERE role='Hr Interviewer' OR role='technical Interviewer'",nativeQuery=true)
    int interviewerDet();   

我的 Pojo 课 -

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="new_user")
public class ClsNew_User   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int users_id;       
    @Column(name="first_name")
    private String first_name;      
    @Column(name="last_name")
    private String last_name;   
    @Column(name="role")
    private String role;    
    @Column(name="select_technical_skill")
    private String select_technical_skill;  
    @Column(name="additional_skills")
    private String additional_skills;
    public int getUsers_id() 
        return users_id;
    
    public void setUsers_id(int users_id) 
        this.users_id = users_id;
    
    public String getFirst_name() 
        return first_name;
    
    public void setFirst_name(String first_name) 
        this.first_name = first_name;
    
    public String getLast_name() 
        return last_name;
    
    public void setLast_name(String last_name) 
        this.last_name = last_name;
    
    public String getRole() 
        return role;
    
    public void setRole(String role) 
        this.role = role;
    
    public String getSelect_technical_skill() 
        return select_technical_skill;
    
    public void setSelect_technical_skill(String select_technical_skill) 
        this.select_technical_skill = select_technical_skill;
    
    public String getAdditional_skills() 
        return additional_skills;
    
    public void setAdditional_skills(String additional_skills) 
        this.additional_skills = additional_skills;
       

而且,它给出的错误是 -

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;嵌套异常是 org.hibernate.tool.schema.spi.SchemaManagementException: Export identifier [new_user] 遇到了不止一次 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.12.jar:5.3.12] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.12.jar:5.3.12] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.6.jar:2.5.6] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.6.jar:2.5.6] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6] 在 com.erecruitment.app.ErecruitmentApplication.main(ErecruitmentApplication.java:10) ~[classes/:na] 引起:javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;嵌套异常是 org.hibernate.tool.schema.spi.SchemaManagementException: Export identifier [new_user] 遇到了不止一次 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.12.jar:5.3.12] 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.12.jar:5.3.12] 在 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.12.jar:5.3.12] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.12.jar:5.3.12] ...省略了16个常用框架 引起:org.hibernate.tool.schema.spi.SchemaManagementException: Export identifier [new_user] 遇到了不止一次 在 org.hibernate.tool.schema.internal.AbstractSchemaMigrator.checkExportIdentifier(AbstractSchemaMigrator.java:490) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:68) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:318) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 在 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.12.jar:5.3.12] 在 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.12.jar:5.3.12] 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.12.jar:5.3.12] ...省略了20个常用框架

如何解决这些错误和异常?

【问题讨论】:

看看有多少@Entity使用@Table(name="new_user") 是的,有两个实体使用相同的表名。错误已解决,谢谢。 【参考方案1】:

Export identifier [X] encountered more than once 的错误原因可能不同。

您应该检查您的标识符“X”,在您的情况下,“new_user”没有被多次使用;例如:同一个表table 'new_user'、table 'new_user'和序列名'new_user'等...

如评论中所述,您的原因是两个具有相同表名的实体。

【讨论】:

以上是关于创建 entityManagerFactory 时出错。无法构建 Hibernate SessionF.SchemaManagementException:导出标识符 [new_user] 遇到多次的主要内容,如果未能解决你的问题,请参考以下文章

启动程序时,在类路径资源中定义名称为 'entityManagerFactory' 的 bean 创建时出错

: 创建类路径资源中定义的名称为“entityManagerFactory”的 bean 时出错

创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“entityManagerFactory”的bean时出错

添加spring boot jpa依赖项时创建名称为“entityManagerFactory”的bean时出错

使用 JPA 进行 Spring 启动 - 创建名为“entityManagerFactory”的 bean 时出错 - driverClassName 需要 jdbcUrl