2016-02-02 109 views
0

我在Spring Boot中編寫簡單的「todo」應用程序。 我在我的開發環境中使用HSQLDB。Hibernate HSQLDB - DuplicateMappingException

我有兩個表格「項目」和「任務」。 任務表對項目表有一對多的參考,因爲我想列出項目中的所有任務。

當我嘗試運行我的應用程序我得到一個異常:

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; 
    } 

} 

這就是我如何查詢數據庫的方式。

ProjectRepository

@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 

回答

0

如果是@ManytoOne那麼字段類型不應該是一個列表,如果你想項目的集合,那麼它應該是@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; 
    } 

} 
+0

我更改爲@OneToMany,它仍然是相同的(org.hibernate.AnnotationException :) – mattsky

+0

我編輯了我的迴應與這兩個實體,請檢查他們是否解決您的問題。 – Rupesh

+0

謝謝你解決了我的問題。可以解釋爲什麼我犯了錯誤? – mattsky

0

@JoinTable試圖創建具有相同名稱的項目的另一個表項。在任務實體替換:

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

@JoinColumn(name = "project_id") 
+0

有新錯誤。我把它粘貼在我的帖子中。 – mattsky

+0

它仍然是DuplicateMappingException?或其他一些恐怖? – Rupesh

+0

這是另一個org.hibernate.AnnotationException。我把它粘貼在我的帖子底部。 – mattsky

0

這個例外很明顯,但有時它是非常具有誤導性的,處理我有同樣的問題,但解決它是在@@@痛苦。

我的項目依賴於另一個項目,這兩個項目都有相同的實體,所以我改變了我的實體,但是hibernate正在檢測另一個項目。我意識到這一點時,我刪除了我的實體和冬眠不斷拋出異常。

0

我有兩個實體類引用相同的表。我只使用一個文件。它沒有任何問題與mysql數據庫,但對於谷歌搜索後,我的東西我通過刪除其他實體類來解決問題。 因此,每個表只保留一個實體類dnt保留兩個,即使出於某種其他原因。

相關問題