java JUnit重复表映射,Exception

时间:2014-02-25 21:27:44

标签: java junit transactions

我正在尝试使用JUnit测试Dao方法:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(transactionManager = "singletable", defaultRollback = false)
@Transactional
public class TestPersonneSingleTableDao {

  @Autowired
  private IHommeSingleTableDao iHommeDao;
  @Autowired
  private IFemmeSingleTableDao iFemmeDao;

  /**
   * 
   */
  @Test
  public void testSavePersonne() {
    // crée et enregistre un homme
    HommeDo hommeDo = new HommeDo();
    hommeDo.setNom("homme1");
    hommeDo.setDatenaissance(new Date());
....
...
}

当我尝试运行它时它会输出我(6次,我有6种测试方法):

févr. 25, 2014 10:12:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Infos: Loading XML bean definitions from class path resource [applicationContext.xml]
févr. 25, 2014 10:12:56 PM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters
Infos: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
févr. 25, 2014 10:12:56 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
Infos: Refreshing org.springframework.context.support.GenericApplicationContext@7c584ba: startup date [Tue Feb 25 22:12:56 CET 2014]; root of context hierarchy
févr. 25, 2014 10:12:56 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
Infos: Loading properties file from class path resource [database.properties]
févr. 25, 2014 10:12:56 PM org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
Infos: Building JPA container EntityManagerFactory for persistence unit 'springmvcapp_pu_singletable'
févr. 25, 2014 10:12:56 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
févr. 25, 2014 10:12:56 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: springmvcapp_pu_singletable
    ...]
févr. 25, 2014 10:12:56 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
févr. 25, 2014 10:12:56 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
févr. 25, 2014 10:12:56 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@71a67848: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,femmeJoinedJpaDao,femmeSingleTableJpaDao,femmeTablePerClassJpaDao,hommeJoinedJpaDao,hommeSingleTableJpaDao,hommeTablePerClassJpaDao,ServicePersonneJoined,ServicePersonneSingleTable,ServicePersonneTablePerClass,createController,deleteController,helloController,readController,createControllerREST,readControllerREST,updateController,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSourceSingleTable,entityManagerFactorySingleTable,transactionManagerSingleTable,dataSourceTablePerClass,entityManagerFactoryTablePerClass,transactionManagerTablePerClass,dataSourceJoined,entityManagerFactoryJoined,transactionManagerJoined,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
févr. 25, 2014 10:12:56 PM org.springframework.test.context.TestContextManager prepareTestInstance
Grave: Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@6a3af151] to prepare test instance [ili.dao.TestPersonneSingleTableDao@7d5cda11]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactorySingleTable' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: springmvcapp_pu_singletable] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:922)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 25 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: springmvcapp_pu_singletable] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:119)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:150)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:67)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 40 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate table mapping femme
    at org.hibernate.cfg.Configuration$MappingsImpl.addDenormalizedTable(Configuration.java:2918)
    at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:237)
    at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:287)
    at org.hibernate.cfg.annotations.EntityBinder.bindTable(EntityBinder.java:568)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:659)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    ... 49 more

它说我“引起:org.hibernate.DuplicateMappingException:复制表映射女性”,但我只有这3个数据对象:

@Pojo
@Entity(name = "PersonneSingleTableDo")
@Table(name = "personne")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "sexe", discriminatorType = DiscriminatorType.STRING)
public abstract class PersonneDo {

  @Id
  @GeneratedValue
  @Column(name = "id")
  private int              id;
  @Column(name = "nom")
  private String           nom;
  @Column(name = "datenaissance")
  private Date             datenaissance;
  @ManyToMany
  @JoinTable(name = "amis", joinColumns = @JoinColumn(name = "idPersonne"), inverseJoinColumns = @JoinColumn(name = "idAmi"))
  private List<PersonneDo> amis;
  @ManyToMany
  @JoinTable(name = "amis", joinColumns = @JoinColumn(name = "idAmi"), inverseJoinColumns = @JoinColumn(name = "idPersonne"))
  private List<PersonneDo> amiDe;

...
getters/setters
...



@Pojo
@Entity(name = "HommeSingleTableDo")
//@Table(name = "personne")
@DiscriminatorValue(value = "homme")
public class HommeDo extends PersonneDo {

}



@Pojo
@Entity(name = "FemmeSingleTableDo")
//@Table(name = "personne")
@DiscriminatorValue(value = "femme")
public class FemmeDo extends PersonneDo {

}

我做了什么错了?

PS:@Pojo注释只是一个关心人的自定义验证器。

1 个答案:

答案 0 :(得分:1)

您正在使用已经具有映射到表'@Table(name =“personne”)'的Abstract类。
由于你继承了子类FemmeDo,它仍然有表映射到那个。
这就是你得到'重复映射'例外的原因。