beans.factory.BeanCreationException:创建名为“dao”的bean时出错:注入持久性依赖项失败

Posted

技术标签:

【中文标题】beans.factory.BeanCreationException:创建名为“dao”的bean时出错:注入持久性依赖项失败【英文标题】:beans.factory.BeanCreationException: Error creating bean with name 'dao': Injection of persistence dependencies failed 【发布时间】:2016-07-28 11:36:06 【问题描述】:

我正在尝试运行我的项目,但出现以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: MY_P_U] Unable to build EntityManagerFactory
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at test.Test.main(Test.java:9) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: MY_P_U] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:530)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:496)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:657)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:630)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:339)
... 13 more Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MY_P_U] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 26 more Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: org.gestion.infopanne.entities.simplemessage column: genre (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:675)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:697)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:719)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:473)
at org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:65)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1332)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1835)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
... 31 more

顺便说一句,我在开始我的项目之前添加了这些依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>$org.springframework-version</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>$org.springframework-version</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.2.2.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.2.0.FINAL</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.0.FINAL</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.1.0.FINAL</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.0.FINAL</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
    </dependency>

最后,我的 applicationContext.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<bean id="dao" class="org.gestion.infopanne.dao.FonctionsDAOimplem">  </bean>
<bean id="metier" class="org.gestion.infopanne.metier.Fonctionsmetierimplem">
<property name="dao" ref="dao"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">   </property>
<property name="url" value="jdbc:mysql://localhost:3306/panne"> </property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager"  class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitMana ger">
<property name="persistenceXmlLocations">
<list>

<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"></property>
</bean>
<bean id="entityManagerFactory"  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager">    </property>
<property name="persistenceUnitName" value="MY_P_U"></property>
</bean>
<bean id="transactionManager"  class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory">   </property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config></context:annotation-config>

    package org.gestion.infopanne.entities; 
    import java.util.Date; 
    import javax.persistence.DiscriminatorValue; 
    import javax.persistence.Entity; 

    @Entity 
    @DiscriminatorValue("simple") 
    public class simplemessage extends message 

    public simplemessage()  super();  

    public simplemessage(String materiel, String description,String genre, String date) 

     super(materiel, description, genre, date);  

消息类:

package org.gestion.infopanne.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="genre",discriminatorType=DiscriminatorType.STRING)
public class message implements Serializable 
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int idm;
private String materiel;
private String description;
private String genre;
private String date;
@ManyToOne
@JoinColumn(name="idm")
private utilisateur user;
public message() 
    super();
    // TODO Auto-generated constructor stub

public message(String materiel, String description,String genre, String date) 
    super();
    this.materiel = materiel;
    this.description = description;
    this.genre = genre;
    this.date = date;

public int getId() 
    return idm;

public void setId(int id) 
    this.idm = id;

public String getMateriel() 
    return materiel;

public void setMateriel(String materiel) 
    this.materiel = materiel;

public String getDescription() 
    return description;

public void setDescription(String description) 
    this.description = description;

public String getGenre() 
    return genre;

public void setGenre(String genre) 
    this.genre = genre;

public String getDate() 
    return date;

public void setDate(String date) 
    this.date = date;

public utilisateur getUser() 
    return user;

public void setUser(utilisateur user) 
    this.user = user;



【问题讨论】:

请发布简单消息实体 错误提示 实体映射中的重复列:org.gestion.infopanne.entities.simplemessage。你上过这个课吗?编辑您的问题并添加类 simplemessage 的代码 包 org.gestion.infopanne.entities;导入 java.util.Date;导入 javax.persistence.DiscriminatorValue;导入 javax.persistence.Entity; @Entity @DiscriminatorValue("simple") public class simplemessage extends message public simplemessage() super(); public simplemessage(字符串材料,字符串描述,字符串流派,字符串日期)超级(材料,描述,流派,日期); 【参考方案1】:

根据这两个链接:

http://www.springbyexample.org/examples/jpa-joined-inheritance-jpa-entity-config.html

https://en.wikibooks.org/wiki/Java_Persistence/Inheritance

我认为你必须从消息类中删除这一行:

private String genre;

及其 getter 和 setter 方法,因为您刚刚使用 DiscriminationColumn 注释定义了此列:

@DiscriminatorColumn(name="genre",discriminatorType=DiscriminatorType.STRING)

【讨论】:

【参考方案2】:

当您使用@DiscriminatorColumn / @DiscriminatorValue 时,您基本上是在告诉JPA,如果该列具有这样的值,那么它就是上述类型(在这种情况下为SimpleMessage)。因此,如果您有SimpleMessage 类型的类,那么您已经知道genre 的值将是simple。因此,您不需要 getter 即可知道 genre 列的值。

但是,假设由于某种原因您需要 JPA 实体中的该字段,该字段必须是不可变的,即您不能更改值,因为更改值意味着它不再属于 SimpleMessage 类型,并且您的代码在那时中断。因此,为了强制您使 genre 不可变,您必须将其标记为不可变,否则实体将无效。

所以在这种情况下,在Message类中genre的定义前添加如下注解

@Column(insert="false", update="false")
private String genre;

【讨论】:

以上是关于beans.factory.BeanCreationException:创建名为“dao”的bean时出错:注入持久性依赖项失败的主要内容,如果未能解决你的问题,请参考以下文章

Spring 3+ Hibernate 4 entityInterceptor 未注入 transactionManager