休眠 HSQLDB - DuplicateMappingException

Posted

技术标签:

【中文标题】休眠 HSQLDB - DuplicateMappingException【英文标题】:Hibernate HSQLDB - DuplicateMappingException 【发布时间】:2016-02-02 19:45:15 【问题描述】:

我正在 Spring Boot 中编写简单的“待办事项”应用程序。 我在我的开发环境中使用 HSQLDB。

我有两个表“项目”和“任务”。 任务表对项目表有一对多的引用,因为我想列出项目中的所有任务。

当我尝试运行我的应用时出现异常:

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 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:768) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1170) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1159) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at pl.mattscode.taskman.TaskmanApplication.main(TaskmanApplication.java:10) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 16 common frames omitted
Caused by: org.hibernate.DuplicateMappingException: Same physical table name [project] references several logical table names: [project], [Project]
    at org.hibernate.cfg.Configuration$MappingsImpl.addTableBinding(Configuration.java:3172) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:321) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:339) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.annotations.EntityBinder.bindTable(EntityBinder.java:594) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:677) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    ... 24 common frames omitted

这是我的休眠实体类:

项目

@Entity
@Table(name="project")
public class Project 
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "project_id")
  private Long projectId;

  @Column
  private String name;

  public Long getId() 
      return projectId;
  

  public void setId(Long id) 
     this.projectId = id;
  

  public String getName() 
    return name;
  

  public void setName(String name) 
     this.name = name;
  

任务

 @Entity
  public class Task 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @ManyToOne
    @JoinColumn(name = "project_id")
    private List<Project> projectId;

    public List<Project> getProjectId() 
        return projectId;
    

    public void setProjectId(List<Project> projectId) 
        this.projectId = projectId;
    

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    


这就是我查询数据库的方式。

项目库

@Repository
public interface ProjectRepository extends JpaRepository<Project, Long> 
    @Query("select t.name from Task t inner join Project p on t.id = p.id")
    List<Task> getProjectsWithTasks();

谁能帮我解决这个问题?

编辑: 我添加了@Table(name="project"),将@JoinTable 更改为@JoinColumn,我还有另一个错误:

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on pl.mattscode.taskman.task.repository.entity.Task.projectId references an unknown entity: java.util.List

【问题讨论】:

【参考方案1】:

如果是@ManytoOne那么字段类型不应该是列表,如果你想要一个Projects的集合那么应该是@OneToMany注解

如果你想试试,我已经修改了文件。

@Entity
public class Project

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "project_id")
    private Long projectId;

    @Column
    private String name;

    @OneToMany
    @JoinColumn(name = "task_id")
    private List<Task> task;

    public List<Task> getTask()
    
        return task;
    

    public void setTask(final List<Task> task)
    
        this.task = task;
    

    public Long getId()
    
        return projectId;
    

    public void setId(final Long id)
    
        this.projectId = id;
    

    public String getName()
    
        return name;
    

    public void setName(final String name)
    
        this.name = name;
    

@Entity
public class Task

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    public Long getId()
    
        return id;
    

    public void setId(final Long id)
    
        this.id = id;
    

    public String getName()
    
        return name;
    

    public void setName(final String name)
    
        this.name = name;
    


【讨论】:

我改成@OneToMany还是一样(org.hibernate.AnnotationException:) 我已经编辑了这两个实体的回复,请检查它们是否解决了您的问题。 感谢您解决了我的问题。能解释一下我犯错的原因吗? 您的要求是向您的项目添加多个任务,因此它必须是 OnetoMany on Project 和任务列表。此外,当您的实体名称与表名相同时,您不需要表注释。【参考方案2】:

@JoinTable 正在尝试创建另一个具有相同名称 Project 的表条目。在您的任务实体中替换:

@ManyToOne
@JoinTable(name="project",
joinColumns = @JoinColumn(name = "project_id"))

@JoinColumn(name = "project_id")

【讨论】:

还是 DuplicateMappingException 吗?还是其他错误? 这是另一个 org.hibernate.AnnotationException。我把它贴在我的帖子底部。【参考方案3】:

我有两个实体类引用同一个表。我只使用一个文件。它对 mysql db 没有任何问题,但对于 oracle 在谷歌搜索后我通过删除其他实体类解决了这个问题。 因此,每个表只保留一个实体类,即使出于其他原因也不要保留两个。

【讨论】:

以上是关于休眠 HSQLDB - DuplicateMappingException的主要内容,如果未能解决你的问题,请参考以下文章

与 HSQLDB 的休眠连接

hsqldb休眠持久化@Lob

在休眠配置文件中设置 HSQLDB 数据类型?

休眠和 HSQLDB - 适用于 PC - Linux 上的错误

对 hsqldb 运行休眠时无法解析的日期

运行程序后文件数据库中没有模式通过休眠持久化到 hsqldb:file