IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[Hibernate 3]Exception avec Polymorphisme : Hibernate veut instancier l'interface!


Sujet :

Hibernate Java

  1. #1
    Membre chevronné
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    940
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 940
    Points : 1 817
    Points
    1 817
    Par défaut [Hibernate 3]Exception avec Polymorphisme : Hibernate veut instancier l'interface!
    Bonjour,

    J'ai 2 classes qui implémentent une interface. J'essaye d'utiliser le polymorphisme d'Hibernate pour pouvoir manipuler mes données à l'aide de l'interface : je souhaite qu'en faisant une requête sur l'interface, Hibernate renvoie les objets des classes l'implémentant.
    Petit rappel :
    http://www.hibernate.org/hib_docs/v3...heritance.html
    Le même en anglais :
    http://www.hibernate.org/hib_docs/v3...heritance.html
    Mais je déclenche une exception car Hibernate essaye d'instancier l'interface elle-même! Voici le fichier de mapping.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
     
    <?xml version="1.0"?>
     
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mapping>
    <class name="com.entreprise.gestapp.backend.model.ChampDemandeInterface" abstract="true">
        <meta attribute="class-description" inherit="false">
            @hibernate
        </meta>
     
        <composite-id>
            <meta attribute="class-description" inherit="false">
               @hibernate.id
                generator-class="assigned"
            </meta>
            <!-- bi-directional many-to-one association to ChampEtat -->
            <key-many-to-one
                name="champEtat"
                class="com.entreprise.gestapp.backend.model.ChampEtat"
            >
                <meta attribute="field-description">
                   @hibernate.many-to-one
                    column="cha_cod""
                </meta>
                <column name="cha_cod" />
            </key-many-to-one>
            <!-- bi-directional many-to-one association to Demande -->
            <key-many-to-one
                name="demande"
                class="com.entreprise.gestapp.backend.model.Demande"
            >
                <meta attribute="field-description">
                   @hibernate.many-to-one
                    column="dem_num""
                </meta>
                <column name="dem_num" />
            </key-many-to-one>
        </composite-id>
     
        <union-subclass name="com.entreprise.gestapp.backend.model.ChampDemandeTexte" table="champdemandetexte">
            <property
                name="valueTexte"
                type="java.lang.String"
                column="chd_valuetexte"
                not-null="false"
                length="32"
            >
                <meta attribute="field-description">
                   @hibernate.property
                    column="chd_valuetexte"
                    not-null="false"
                    length="32"
                </meta>    
            </property>
        </union-subclass>
     
        <union-subclass name="com.entreprise.gestapp.backend.model.ChampDemandeDate" table="champdemandedate">
            <property
                name="valueDate"
                type="java.util.Date"
                column="chd_valuedate"
                not-null="false"
            >
                <meta attribute="field-description">
                   @hibernate.property
                    column="chd_valuedate"
                    not-null="false"
                </meta>    
            </property>
        </union-subclass>
     
    </class>
    </hibernate-mapping>
    et le code effectuant la requête :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Criteria crit = getSession().createCriteria(ChampDemandeInterface.class);
    List listDemandeChampsModel = crit.list();
    J'ai enlevé tous les critères pour rendre le code plus lisible, j'ai d'ailleurs testé le code ainsi.

    J'obtiens alors l'exception suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
     org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: com.entreprise.gestapp.backend.model.ChampDemandeInterface
        at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:56)
        at org.hibernate.tuple.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:89)
        at org.hibernate.type.ComponentType.instantiate(ComponentType.java:345)
        at org.hibernate.type.ComponentType.instantiate(ComponentType.java:351)
        at org.hibernate.type.EmbeddedComponentType.instantiate(EmbeddedComponentType.java:52)
        at org.hibernate.type.ComponentType.resolve(ComponentType.java:441)
        at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:182)
        at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:759)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:292)
        at org.hibernate.loader.Loader.doQuery(Loader.java:412)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
        at org.hibernate.loader.Loader.doList(Loader.java:1593)
        at org.hibernate.loader.Loader.list(Loader.java:1577)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1322)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
        at com.entreprise.gestapp.backend.dao.DemandeDao.getListDemandeChamps(DemandeDao.java:762)
        at com.entreprise.gestapp.backend.dao.DemandeDao.transformModelToTransport(DemandeDao.java:148)
        at com.entreprise.gestapp.backend.dao.DemandeDao.transformModelToTransportWithLibelle(DemandeDao.java:244)
        at com.entreprise.gestapp.backend.dao.DemandeDao.getListDemande(DemandeDao.java:522)
        at com.entreprise.gestapp.backend.service.DemandeManager.getListDemande(DemandeManager.java:152)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
        at $Proxy10.getListDemande(Unknown Source)
        at com.entreprise.gestapp.frontend.webapp.action.PortefeuilleListActionBean.getListDemande(PortefeuilleListActionBean.java:308)
    au lieu d'une belle liste contenant des objets de classe ChampDemandeTexte et CHampDemandeDate, les deux classes implémentant l'interface ChampDemandeInterface. ChampDemandeTexte et ChampDemandeTexte sont des classes de données classiques, avec uniquement des getters et des setters.

    J'ai fait la même chose pour une autre hiérarchie interface - classe similaire, le concept lui-même est donc viable. (L'autre hiérarchie utilise cependant une séquence pour créer sa clé primaire, pas deux clés étrangère. J'ignore si c'est important.)

    J'ai essayé de faire une requête directement sur la classe ChampDemandeTexte, sans faire un autre fichier de mapping. Hibernate a quand même essayé d'instancier ChampDemandeInterface!

    J'avoue que je suis perdu. Quelqu'un sait-il ce qui ne va pas?

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    940
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 940
    Points : 1 817
    Points
    1 817
    Par défaut
    J'ai trouvé! La clé (primaire ) était ici :
    Citation Envoyé par BugFactory
    L'autre hiérarchie utilise cependant une séquence pour créer sa clé primaire, pas deux clés étrangère. J'ignore si c'est important.
    C'était important. J'ai ajouté un champ entier alimenté par une séquence que j'utilise comme clé primaire, et mes deux clés étrangère n'en font désormais plus partie. A présent, ça marche.
    D'après mon chef de projet, Hibernate a souvent des problèmes avec les clés multiples.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Points : 635
    Points
    635
    Par défaut
    Hibernate recommande de n'utilise les clés composites que si c'est vraiment nécessaire, par exemple dans le cas où tu ne peux pas modifier la structure de la base de donnes.

  4. #4
    Membre expert Avatar de KiLVaiDeN
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 851
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 851
    Points : 3 481
    Points
    3 481
    Par défaut
    Salut,

    Tu n'as pas définie de type pour tes colonnes composite, me semble-t-il, cela est facheux je pense pour Hibernate

    Perso j'ai utilisé des clés composites à plusieurs reprises sur des projets où en effet il n'y avait pas le choix, et ça fonctionnait à merveille.

    A+
    K

  5. #5
    Membre chevronné
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    940
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 940
    Points : 1 817
    Points
    1 817
    Par défaut
    J'ai utilisé une clé composite similaire pour un autre mapping qui ne faisait pas usage du polymorphisme et ça marchait.
    J'en profite pour ajouter le tag résolu que j'avais oublié.

  6. #6
    Membre expert Avatar de KiLVaiDeN
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 851
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 851
    Points : 3 481
    Points
    3 481
    Par défaut
    Je pense que ça dépend du type de données dans ta base : en integer, ça doit marcher même sans préciser le type, mais si c'est une colonne de type char.. ça peut poser des problèmes.
    K

  7. #7
    Membre chevronné
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    940
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 940
    Points : 1 817
    Points
    1 817
    Par défaut
    Long dans les deux cas. (Pour être précis, des Bigserial dans PostgreSQL.)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Hibernate / Tomcat] Problème avec une exception
    Par ahlam1 dans le forum Tomcat et TomEE
    Réponses: 3
    Dernier message: 28/07/2011, 14h03
  2. Réponses: 0
    Dernier message: 09/04/2011, 14h41
  3. Réponses: 7
    Dernier message: 09/04/2010, 18h48
  4. fatal exception avec hibernate
    Par Klemsy78 dans le forum Persistance des données
    Réponses: 5
    Dernier message: 31/10/2007, 17h38
  5. [Hibernate] - Mapping Exception
    Par msiramy dans le forum Hibernate
    Réponses: 3
    Dernier message: 09/01/2006, 16h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo