Spring Boot Data JPA 的问题 - 创建名称为 [重复] 的 bean 时出错

Posted

技术标签:

【中文标题】Spring Boot Data JPA 的问题 - 创建名称为 [重复] 的 bean 时出错【英文标题】:Issue with Spring Boot Data JPA -- Error creating bean with name [duplicate] 【发布时间】:2017-07-15 10:54:36 【问题描述】:

当我尝试编译我的 Spring Boot 应用程序时,我得到了这个异常。请注意,到目前为止,这是一个非常简单的应用程序,所以我不完全确定自己做错了什么。

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:1628) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.nuradin.victorious.Sandbox.main(Sandbox.java:12) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[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.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: match_participant, for columns: [org.hibernate.mapping.Column(masteries)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    ... 22 common frames omitted


Process finished with exit code 1

这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nuradin.sandbox</groupId>
<artifactId>Victorious</artifactId>
<version>0.1.0 - Development</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.tensorflow</groupId>
        <artifactId>tensorflow</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我的配置很可能是错误的。我到处寻找并尝试了许多不同的配置,但它不起作用。有许多不同的帖子与我的例外相同,但第一个下面的行不同,他们的解决方案对我不起作用。

请帮帮我


编辑:这是 MatchParticipants 的来源:

package org.nuradin.victorious.data.matchcrawler;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.List;

@Entity
public class MatchParticipant 

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int entryId;

    private ParticipantStats stats;
    private int participantId;
    private List<Rune> runes;
    private ParticipantTimeline timeline;
    private int teamId;
    private int spell2Id;
    private List<MasteryPoint> masteries;
    private String highestAchievedSeasonTier;
    private int spell1Id;
    private int championId;

    public ParticipantStats getStats() 
        return stats;
    

    public void setStats(ParticipantStats stats) 
        this.stats = stats;
    

    public int getParticipantId() 
        return participantId;
    

    public void setParticipantId(int participantId) 
        this.participantId = participantId;
    

    public List<Rune> getRunes() 
        return runes;
    

    public void setRunes(List<Rune> runes) 
        this.runes = runes;
    

    public ParticipantTimeline getTimeline() 
        return timeline;
    

    public void setTimeline(ParticipantTimeline timeline) 
        this.timeline = timeline;
    

    public int getTeamId() 
        return teamId;
    

    public void setTeamId(int teamId) 
        this.teamId = teamId;
    

    public int getSpell2Id() 
        return spell2Id;
    

    public void setSpell2Id(int spell2Id) 
        this.spell2Id = spell2Id;
    

    public List<MasteryPoint> getMasteries() 
        return masteries;
    

    public void setMasteries(List<MasteryPoint> masteries) 
        this.masteries = masteries;
    

    public String getHighestAchievedSeasonTier() 
        return highestAchievedSeasonTier;
    

    public void setHighestAchievedSeasonTier(String highestAchievedSeasonTier) 
        this.highestAchievedSeasonTier = highestAchievedSeasonTier;
    

    public int getSpell1Id() 
        return spell1Id;
    

    public void setSpell1Id(int spell1Id) 
        this.spell1Id = spell1Id;
    

    public int getChampionId() 
        return championId;
    

    public void setChampionId(int championId) 
        this.championId = championId;
    

【问题讨论】:

能否附上match_participant实体来源? 我已经发布了 MatchParticipant 来源 您是否在 Entity 类中导入 Rune 和 MasteryPoint 类型? 【参考方案1】:

您的所有实体字段都缺少适当的 JPA 注释。例如,您必须声明连接类型:

@OneToMany(mappedBy="match_participant_id")
private List<MasteryPoint> masteries;

每个实际的 db 列字段都应该有 @Column 注释。我建议阅读一些简短的 Hibernate/JPA 教程。例如This one 或任何其他在线可用的。

【讨论】:

谢谢,我现在就试试这个 成功了,谢谢【参考方案2】:

您介意为列masteries 共享match_participant 的JPA 实体吗?有了给定的详细信息,我相信您使用 java.util.List 作为字段类型 masteries 而不是 java.util.List&lt;mastery&gt;

详情请见org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

【讨论】:

我已经发布了 MatchParticipant 来源【参考方案3】:

添加以下依赖项怎么样?

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.4.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.2.3.Final</version>
</dependency>

【讨论】:

【参考方案4】:

您必须在实体之间建立联系。

检查这个: http://www.baeldung.com/hibernate-one-to-many

您可以在实体属性或 set 方法上使用键的注释...

【讨论】:

以上是关于Spring Boot Data JPA 的问题 - 创建名称为 [重复] 的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot-starter-data-jpa 依赖错误

没有 Spring Boot 的 Spring Data JPA

Spring Boot整合Spring Data JPA

spring-boot-starter-data-jpa 以非零退出值 1 结束

Spring Boot Data JPA:休眠会话问题

Spring Boot + Spring Data JPA + 事务无法正常工作