2016-07-28 50 views
0

我試圖運行我的項目,但我得到這些下列情況除外:beans.factory.BeanCreationException:錯誤創建名爲「道」豆:持久性的依賴注入失敗

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

Message類:

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

} 
+0

請張貼SimpleMessage中實體 – amicoderozer

+0

錯誤說*爲實體映射重複列:org.gestion.infopanne。 entities.simplemessage *。你有上課嗎?編輯你的問題,並添加類的代碼simplemessage – amicoderozer

+0

package org.gestion.infopanne.entities; import java.util.Date; import javax.persistence.DiscriminatorValue; import javax.persistence。實體; @Entity @DiscriminatorValue( 「簡單」) 公共類SimpleMessage中延伸消息{ \t公共SimpleMessage中(){ \t \t超級(); \t} \t公共SimpleMessage中(字符串物資,字符串描述,字符串類型,字符串日期){ \t \t超級(物資,描述,流派,日期); \t} } –

回答

1

當您使用@DiscriminatorColumn/@DiscriminatorValue,你基本上是告訴JPA是如果該列具有這樣的值,則它是所述類型(在這種情況下爲SimpleMessage)。所以,如果你有類型SimpleMessage,那麼你已經知道genre的值將是simple。因此,你不需要一個getter來知道genre列的值。

但是,由於某種原因,您需要JPA實體中的該字段,因此要求該字段是不可變的,即您無法更改該值,因爲更改該值意味着它不再是SimpleMessage類型,並且你的代碼在那個時候中斷了。所以爲了強制你使genre不可變,你必須將它標記爲這樣,否則實體將會失效。

因此,在這種情況下,在Messagegenre定義之前添加以下注釋

@Column(insert="false", update="false") 
private String genre; 
相關問題