Spring 3.1 Hibernate 4 exception pour l’inheritance

Bonjour, je viens de commencer à utiliser Spring, avec Hibernate4 et Maven. Fondamentalement, ma hiérarchie de classes est HUmanMicroTask, qui s’étend de MicroTask. À l’avenir, plusieurs autres classes pourraient s’étendre depuis MicroTask. J’essayais d’avoir une table par classe concrète, ce qui est la manière la plus simple de fonctionner avec spring3 et hibernate 4. Cependant, lorsque je lance mon code. Je continue à recevoir l’exception suivante

13:11:52,260 ERROR TestContextManager:324 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6ef137d] to prepare test instance [HumanMicroTaskBaseHibernateTest@52c05d3b] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) 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:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 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:300) 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 'sessionFactory' defined in class path resource [database-config.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass 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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1) at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) ... 24 more Caused by: java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:212) at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:203) at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2013) at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:768) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:687) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3431) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3385) at org.hibernate.cfg.Configuration.secondPassComstack(Configuration.java:1337) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1727) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778) at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184) at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) ... 38 more 

Bien que j’aie vu pas mal de forums, je ne peux pas décider où je commets l’erreur. Ma classe MicroTask se présente comme suit:

 @Entity @Table(name = "MICROTASK") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class MicroTask { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") @Column(name = "MICROTASKID") private Ssortingng microTaskId; @Column(name = "CREATIONDATE") private Date creationDate; @Column(name = "DESCRIPTION") private Ssortingng description; public Date getCreationDate() { return creationDate; } //More Getters and setters 

Ma classe HumanMicroTask se présente comme suit:

  @Entity @Table(name = "HUMANMICROTASK") @AtsortingbuteOverrides({ @AtsortingbuteOverride(name="microTaskId", column=@Column(name="MICROTASKID")), @AtsortingbuteOverride(name="creationDate", column=@Column(name="CREATIONDATE")), @AtsortingbuteOverride(name="description", column=@Column(name="DESCRIPTION")) }) public class HumanMicroTask extends MicroTask { @Column(name = "TITLE") private Ssortingng title; @Column(name = "CHANNEL") private Ssortingng channel; @Id @Column(name = "HMTID") private Ssortingng humanMicroTaskid; public Ssortingng getId() { return humanMicroTaskid; } //More Getters and setters 

Et mon config.xml se présente comme suit:

    ${jdbc.driver.className}   ${jdbc.url}   ${jdbc.username}   ${jdbc.password}          ${jdbc.hibernate.dialect} create  create true           

Ma configuration Maven

  true 3.1.0.RELEASE 4.1.1.Final 1.5.6 

Merci de m’aider. Je ne suis pas sûr de savoir où je fais l’erreur.

    C’est dû à la colonne Id dans les deux classes. Supprimez l’ID de HumanMicroTask.

    pour résoudre ce problème Supprimer @Id de la sous-classe

    dans MicroTask keep

      @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") @Column(name = "MICROTASKID") private Ssortingng microTaskId; 

    dans la sous-classe HumanMicroTask supprimer

      @Id @Column(name = "HMTID") private Ssortingng humanMicroTaskid; 

    J’ai eu le même problème il y a quelque temps, puisque votre classe parente a une clé primaire: “Id”, lorsque les sous-classes sont générées, elles génèrent automatiquement une clé étrangère avec le nom exact de la clé primaire de leur parent

    Exemple: (pseudocode)

    Définition d’entité

    Classe parent

      @Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name = "abstract_person", catalog = "catalog", schema = "") class AbstractPerson{ //Primary Key @Id @Column(name = "idPerson") int idPerson; @Basic @Column(name = "name") Ssortingng name; //corresponding getters and setters } 

    Classe enfant:

      @Entity @Table(name = "concrete_person", catalog = "catalog", schema = "") class ConcretePerson extends AbstractPerson{ //No id or primary key is defined here @Basic @Column(name="profession") Ssortingng profession; } 

    Génération de table

    La classe des parents correspondra à cette

    Tableau “abstract_person”
    id: Int (clé primaire)
    nom: Varchar

    La classe enfant correspondra à ceci:

    Tableau “concrete_person”
    profession: Varchar
    idPerson: int (généré automatiquement, clé étrangère à la table parent et classe primaire de cette table)

    //Hypothèses
    Base de données Mysql;
    Mise en œuvre d’Hibernate Jpa 2;
    NetBeans 7x Ide