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 :

Questions JPA Hibernate


Sujet :

Hibernate Java

  1. #1
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut Questions JPA Hibernate
    Bonjour,

    J'essaye de mieux comprendre les annotations avec JPA/Hibernate et SQLServer. J'ai créé un projet simple : une classe abstraite nommée "Articles". Deux classes en héritent : Ramette qui ajoute un grammage et Stylo qui ajoute une couleur. Le code ci-dessous ne fonctionne pas et je ne parviens pas à corriger les erreurs. Auriez-vous une idée ? Merci !

    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
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    package fr.eni.hibernate.entities;
     
    import java.io.Serializable;
     
    import javax.persistence.Column;
    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.Table;
     
    @Entity
    @Table(name = "Articles")
    @Inheritance( strategy = InheritanceType.SINGLE_TABLE )
    @DiscriminatorColumn( name="type", discriminatorType = DiscriminatorType.STRING)
    public abstract class Articles implements Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	@Column(name = "idarticle")
    	private Integer idarticle;
    	@Column(name = "reference")
    	private String reference;
    	@Column(name = "marque")
    	private String marque ;
    	@Column(name = "designation")
    	private String designation;
    	@Column(name = "prixUnitaire")
    	private float prixUnitaire ;
    	@Column(name = "qteStock")
    	private int qteStock ;
     
    	public Articles() {
    	}
     
     
     
    	public Integer getIdArticle() {
    		return idarticle;
    	}
     
    	public String getReference() {
    		return reference;
    	}
     
    	public void setReference(String reference) {
    		this.reference = reference;
    	}
     
    	public String getMarque() {
    		return marque;
    	}
     
    	public void setMarque(String marque) {
    		this.marque = marque;
    	}
     
    	public String getDesignation() {
    		return designation;
    	}
     
    	public void setDesignation(String designation) {
    		this.designation = designation;
    	}
     
    	public float getPrixUnitaire() {
    		return prixUnitaire;
    	}
     
    	public void setPrixUnitaire(float prixUnitaire) {
    		this.prixUnitaire = prixUnitaire;
    	}
     
    	public int getQteStock() {
    		return qteStock;
    	}
     
    	public void setQteStock(int qteStock) {
    		this.qteStock = qteStock;
    	}
     
    	@Override
    	public String toString() {
    		return "Article [idArticle=" + idarticle + ", reference=" + reference + ", marque=" + marque + ", designation="
    				+ designation + ", prixUnitaire=" + prixUnitaire + ", qteStock=" + qteStock + "]";
    	}
     
    }
    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
    package fr.eni.hibernate.entities;
     
    import javax.persistence.Column;
    import javax.persistence.DiscriminatorValue;
    import javax.persistence.Entity;
     
    @Entity
    @DiscriminatorValue("Ramette")
    public class Ramette extends Articles {
     
     
    	private static final long serialVersionUID = 1L;
    	private int grammage;
     
    	public Ramette() {
     
    	}
     
     
     
    	@Column(name = "grammage")
    	public int getGrammage() {
    		return grammage;
    	}
     
    	public void setGrammage(int grammage) {
    		this.grammage = grammage;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() + " Ramette [grammage=" + grammage + "]";
    	}
     
    }
    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
    package fr.eni.hibernate.entities;
     
    import javax.persistence.Column;
    import javax.persistence.DiscriminatorValue;
    import javax.persistence.Entity;
     
    @Entity
    @DiscriminatorValue("Stylo")
    public class Stylo extends Articles {
     
     
    	private static final long serialVersionUID = 1L;
    	private String couleur;
     
    	public Stylo() {
     
    	}
     
     
     
    	@Column(name = "couleur")
    	public String getCouleur() {
    		return couleur;
    	}
     
    	public void setCouleur(String couleur) {
    		this.couleur = couleur;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() + " Stylo [couleur=" + couleur + "]";
    	}
     
    }
    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
    package fr.eni.hibernate.entities;
     
    import java.util.List;
     
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.persistence.TypedQuery;
     
    public class Main {
     
    	public static void main(String[] args) throws Exception {
     
    		EntityManagerFactory entityManagerFactory = null;
    		EntityManager entityManager = null;
    		try {
    			entityManagerFactory = Persistence.createEntityManagerFactory("WebStore");
    			entityManager = entityManagerFactory.createEntityManager();
     
    			TypedQuery<Articles> query = entityManager.createQuery("from Articles", Articles.class);
    			List<Articles> art = query.getResultList();
    			for (Articles article : art) {
    				System.out.println(art.getClass().getName());
    				System.out.println("\t" + article);
    			}
     
    		} finally {
    			if (entityManager != null)
    				entityManager.close();
    			if (entityManagerFactory != null)
    				entityManagerFactory.close();
    		}
    	}
    }
    Code XML : 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
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    	version="2.0">
     
    	<persistence-unit name="WebStore">
    		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
     
    		<class>fr.eni.hibernate.entities.Articles</class>
                    <class>fr.eni.hibernate.entities.Stylo</class>
                    <class>fr.eni.hibernate.entities.Ramette</class>
     
     
    		<properties>
    			<property name="javax.persistence.jdbc.driver"
    				value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    			<property name="javax.persistence.jdbc.url"
    				value="jdbc:sqlserver://localhost;database=PAPETERIE_TEST" />
    			<property name="javax.persistence.jdbc.user" value="sa" />
    			<property name="javax.persistence.jdbc.password" value="x" />
     
    			<property name="hibernate.dialect"
    				value="org.hibernate.dialect.SQLServerDialect" />
    			<property name="hibernate.format_sql" value="false" />
    		</properties>
    	</persistence-unit>
     
    </persistence>

    Code SQL : 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
    CREATE TABLE Articles(
    	idarticle INT IDENTITY(1,1),
    	reference varchar(10) NOT NULL,
    	marque nvarchar(200) NOT NULL,
    	designation nvarchar(250) NOT NULL,
    	prixUnitaire float NOT NULL,
    	qteStock int NOT NULL,
    	grammage int NULL,
    	couleur nvarchar(50) NULL,
    	type nchar(10) NOT NULL,
     
    	CONSTRAINT PK_Articles PRIMARY KEY (idarticle)
    )
     
    INSERT INTO Articles (reference, marque, designation, prixUnitaire, qteStock, grammage, couleur, type) VALUES 
    ('Bic', 'BBOrange', 'Bic bille Orange', 1.2, 20, 0, 'Bleu', 'Stylo'),
    ('Bic', 'BBOrange', 'Bic bille Orange', 1.2, 20, 0,'noir', 'Stylo'),
    ('Clairef', 'CRA4S', 'Ramette A4 Sup', 9, 20, 80, null, 'Ramette');


    Voici l'erreur qui s'affiche : Object [id=1] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo

  2. #2
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Le grammage est propre à la ramette, et non au stylo. Je pense que on doit avoir null pour le grammage dans le cas d'un stylo.

    https://stackoverflow.com/questions/...ified-subclass

    https://forum.hibernate.org/viewtopic.php?p=2409570

  3. #3
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par PhilippeGibault Voir le message
    Le grammage est propre à la ramette, et non au stylo. Je pense que on doit avoir null pour le grammage dans le cas d'un stylo.

    https://stackoverflow.com/questions/...ified-subclass

    https://forum.hibernate.org/viewtopic.php?p=2409570
    J'ai essayé, mais j'ai toujours la même erreur

  4. #4
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    INSERT INTO Articles (reference, marque, designation, prixUnitaire, qteStock, grammage, couleur, type) VALUES 
                                   ('Bic','BBOrange', 'Bic bille Orange', 1.2, 20, null, 'Bleu', 'Stylo'),
                                  ('Bic', 'BBOrange', 'Bic bille Orange', 1.2, 20, null,'noir', 'Stylo'),
                                  ('Clairef', 'CRA4S', 'Ramette A4 Sup', 9, 20, 80, null, 'Ramette');
    ?????

  5. #5
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par PhilippeGibault Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    INSERT INTO Articles (reference, marque, designation, prixUnitaire, qteStock, grammage, couleur, type) VALUES 
                                   ('Bic','BBOrange', 'Bic bille Orange', 1.2, 20, null, 'Bleu', 'Stylo'),
                                  ('Bic', 'BBOrange', 'Bic bille Orange', 1.2, 20, null,'noir', 'Stylo'),
                                  ('Clairef', 'CRA4S', 'Ramette A4 Sup', 9, 20, 80, null, 'Ramette');
    ?????
    Oui, c'est bien cette requête que j'ai exécutée. Voici l'erreur :

    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
     
    nov. 18, 2020 1:37:58 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
    INFO: HHH000204: Processing PersistenceUnitInfo [name: Articles]
    nov. 18, 2020 1:37:58 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate ORM core version 5.4.23.Final
    nov. 18, 2020 1:37:58 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
    INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
    nov. 18, 2020 1:37:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
    nov. 18, 2020 1:37:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost;database=PAPETERIE_TEST]
    nov. 18, 2020 1:37:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001001: Connection properties: {password=****, user=sa}
    nov. 18, 2020 1:37:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001003: Autocommit mode: false
    nov. 18, 2020 1:37:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
    INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
    nov. 18, 2020 1:37:58 PM org.hibernate.dialect.Dialect <init>
    INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
    nov. 18, 2020 1:37:59 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
    INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
    nov. 18, 2020 1:38:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
    INFO: HHH10001008: Cleaning up connection pool [jdbc:sqlserver://localhost;database=PAPETERIE_TEST]
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object [id=15] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    	at org.hibernate.query.Query.getResultList(Query.java:165)
    	at fr.eni.hibernate.entities.Main.main(Main.java:22)
    Caused by: org.hibernate.WrongClassException: Object [id=15] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1945)
    	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1726)
    	at org.hibernate.loader.Loader.getRow(Loader.java:1623)
    	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    	at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:959)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    	at org.hibernate.loader.Loader.doList(Loader.java:2850)
    	at org.hibernate.loader.Loader.doList(Loader.java:2832)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    	at org.hibernate.loader.Loader.list(Loader.java:2659)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    	... 2 more

  6. #6
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Essaye
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    SELECT * FROM Articles
    Que ce passe-t-il si on insert:
    Seulement Ligne 1?
    Seulement Ligne 2?
    Seulement Ligne 3?
    Seulement Ligne 1 et 2?

  7. #7
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par PhilippeGibault Voir le message
    Essaye
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    SELECT * FROM Articles
    Que ce passe-t-il si on insert:
    Seulement Ligne 1?
    Seulement Ligne 2?
    Seulement Ligne 3?
    Seulement Ligne 1 et 2?
    Voici l'affichage :

    Ligne 1 seulement :

    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
     
     
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object [id=18] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    	at org.hibernate.query.Query.getResultList(Query.java:165)
    	at fr.eni.hibernate.entities.Main.main(Main.java:22)
    Caused by: org.hibernate.WrongClassException: Object [id=18] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1945)
    	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1726)
    	at org.hibernate.loader.Loader.getRow(Loader.java:1623)
    	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    	at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:959)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    	at org.hibernate.loader.Loader.doList(Loader.java:2850)
    	at org.hibernate.loader.Loader.doList(Loader.java:2832)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    	at org.hibernate.loader.Loader.list(Loader.java:2659)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    	... 2 more

    Ligne 2 seulement :

    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
     
     
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object [id=19] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    	at org.hibernate.query.Query.getResultList(Query.java:165)
    	at fr.eni.hibernate.entities.Main.main(Main.java:22)
    Caused by: org.hibernate.WrongClassException: Object [id=19] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1945)
    	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1726)
    	at org.hibernate.loader.Loader.getRow(Loader.java:1623)
    	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    	at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:959)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    	at org.hibernate.loader.Loader.doList(Loader.java:2850)
    	at org.hibernate.loader.Loader.doList(Loader.java:2832)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    	at org.hibernate.loader.Loader.list(Loader.java:2659)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    	... 2 more
    Ligne 3 seulement :

    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
     
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object [id=20] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Ramette   
    	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    	at org.hibernate.query.Query.getResultList(Query.java:165)
    	at fr.eni.hibernate.entities.Main.main(Main.java:22)
    Caused by: org.hibernate.WrongClassException: Object [id=20] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Ramette   
    	at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1945)
    	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1726)
    	at org.hibernate.loader.Loader.getRow(Loader.java:1623)
    	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    	at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:959)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    	at org.hibernate.loader.Loader.doList(Loader.java:2850)
    	at org.hibernate.loader.Loader.doList(Loader.java:2832)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    	at org.hibernate.loader.Loader.list(Loader.java:2659)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    	... 2 more
    Ligne 1 et 2 :

    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
     
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object [id=21] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    	at org.hibernate.query.Query.getResultList(Query.java:165)
    	at fr.eni.hibernate.entities.Main.main(Main.java:22)
    Caused by: org.hibernate.WrongClassException: Object [id=21] was not of the specified subclass [fr.eni.hibernate.entities.Articles] : Discriminator: Stylo     
    	at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1945)
    	at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1726)
    	at org.hibernate.loader.Loader.getRow(Loader.java:1623)
    	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:740)
    	at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1039)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:959)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    	at org.hibernate.loader.Loader.doList(Loader.java:2850)
    	at org.hibernate.loader.Loader.doList(Loader.java:2832)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
    	at org.hibernate.loader.Loader.list(Loader.java:2659)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    	... 2 more

  8. #8
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Et il y a quoi dans la BDD?

  9. #9
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Citation Envoyé par PhilippeGibault Voir le message
    Et il y a quoi dans la BDD?
    Voici l'affichage dans la BDD

    Ligne 1 seulement :

    Nom : ligne1.png
Affichages : 258
Taille : 5,0 Ko

    Ligne 2 seulement :

    Nom : ligne2.png
Affichages : 258
Taille : 4,8 Ko

    Ligne 3 seulement :

    Nom : ligne3.png
Affichages : 264
Taille : 4,8 Ko

    Ligne 1 et 2 :


    Nom : ligne1et2.png
Affichages : 260
Taille : 6,8 Ko

  10. #10
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Bonjour,

    Là, je ne vois vraiment pas où est le problème (et il y en a effectivement 1).

    Ce que je propose, c'est de tout reprendre par étape.

    1. Je ne mets qu'une seule classe (comme Article). Je vérifie que ça marche. Je ne mets même pas d'héritage (donc pas de colonne discriminante).
    2. Je crée la classe suivante (comme Grammage) avec la colonne discriminante et l'héritage.
    Le mérite est de savoir où ça va merder.

    Autre proposition:
    D'habitude, je passe par Spring (et non persistance.xml). Faut-il spécifier la classe Articles dedans?

    Autre proposition:
    Plus pour voir la cohérence de la BDD avec le modèle, vide la table articles en BDD et insère tout par JPA. On verra si il y a un problème ou pas ici.

    Je m'excuse de ne pas avoir répondu avant, j'étais un peu pris.

    Cordialement.

  11. #11
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    Merci pour votre message. J'essaie de résoudre le problème avec Spring Boot. Voici où j'en suis pour l'instant :

    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
    package com.example.demo.controller;
     
    import java.util.List;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    import com.example.demo.model.ArticleInfo;
     
    @Controller
    public class AppController {
     
    	@Autowired
    	private ArticleService service;
     
    	@RequestMapping("/")
    	public String viewHomePage(Model model) {
    		List<ArticleInfo> listArticles = service.listAll();
    		model.addAttribute("listArticles", listArticles);
     
    		return "index";
    	}
     
    }
    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
    package com.example.demo.controller;
     
    import java.util.List;
     
    import javax.transaction.Transactional;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
     
    import com.example.demo.dao.IArticleRepository;
    import com.example.demo.model.ArticleInfo;
     
    @Service
    @Transactional
    public class ArticleService {
     
    	@Autowired
    	private IArticleRepository repo;
     
    	public List<ArticleInfo> listAll() {
    		return repo.findAll();
    	}
     
    	public void save(ArticleInfo article) {
    		repo.save(article);
    	}
     
    	public ArticleInfo get(Integer id) {
    		return repo.findById(id).get();
    	}
     
    	public void delete(Integer id) {
    		repo.deleteById(id);
    	}
    }

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package com.example.demo.dao;
     
    import org.springframework.data.jpa.repository.JpaRepository;
     
    import com.example.demo.model.ArticleInfo;
     
    public interface IArticleRepository extends JpaRepository<ArticleInfo, Integer> {
     
    }

    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
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    package com.example.demo.model;
     
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
     
    @Entity
    @Table(name = "Articles")
    public class ArticleInfo {
     
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Integer id;
    	private String reference;
    	private String marque;
    	private String designation;
    	private int prixUnitaire;
    	private int qteStock;
    	private String couleur;
    	private int grammage;
     
    	public ArticleInfo(String reference, String marque, String designation, int prixUnitaire, int qteStock) {
    		this.reference = reference;
    		this.marque = marque;
    		this.designation = designation;
    		this.prixUnitaire = prixUnitaire;
    		this.qteStock = qteStock;
    	}
     
    	public ArticleInfo(Integer id, String reference, String marque, String designation, int prixUnitaire,
    			int qteStock, String couleur) {
    		this(reference, marque, designation, prixUnitaire, qteStock);
    		this.id = id;
    		this.couleur = couleur;
    	}
     
    	public ArticleInfo(Integer id, String reference, String marque, String designation, int prixUnitaire,
    			int qteStock, int grammage) {
    		this(reference, marque, designation, prixUnitaire, qteStock);
    		this.id = id;
    		this.grammage = grammage;
    	}
     
    	public Integer getId() {
    		return id;
    	}
     
    	public void setIdArticle(Integer id) {
    		this.id = id;
    	}
     
    	public String getReference() {
    		return reference;
    	}
     
    	public void setReference(String reference) {
    		this.reference = reference;
    	}
     
    	public String getMarque() {
    		return marque;
    	}
     
    	public void setMarque(String marque) {
    		this.marque = marque;
    	}
     
    	public String getDesignation() {
    		return designation;
    	}
     
    	public void setDesignation(String designation) {
    		this.designation = designation;
    	}
     
    	public int getPrixUnitaire() {
    		return prixUnitaire;
    	}
     
    	public void setPrixUnitaire(int prixUnitaire) {
    		this.prixUnitaire = prixUnitaire;
    	}
     
    	public int getQteStock() {
    		return qteStock;
    	}
     
    	public void setQteStock(int qteStock) {
    		this.qteStock = qteStock;
    	}
     
    	public String getCouleur() {
    		return couleur;
    	}
     
    	public void setCouleur(String couleur) {
    		this.couleur = couleur;
    	}
     
    	public int getGrammage() {
    		return grammage;
    	}
     
    	public void setGrammage(int grammage) {
    		this.grammage = grammage;
    	}
     
    	@Override
    	public String toString() {
    		return "Article [idArticle=" + id + ", reference=" + reference + ", marque=" + marque + ", designation="
    				+ designation + ", prixUnitaire=" + prixUnitaire + ", qteStock=" + qteStock + "]";
    	}
     
    }

    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
    package com.example.demo;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
     
    @SpringBootApplication
    public class SpringBootJdbcApplication {
     
    	public static void main(String[] args) {
    		SpringApplication.run(SpringBootJdbcApplication.class, args);
     
    	}
     
     
     
    }

  12. #12
    Membre éclairé

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    461
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 461
    Points : 894
    Points
    894
    Billets dans le blog
    5
    Par défaut
    Dans ce cas là, la meilleure solution est de vider la BDD et d'ajouter par JPA de la donnée.
    Tu verras si ton mapping est bon et si tu arrives à insérer de la donnée.

    Par ailleurs, avec Spring, et surtout Spring boot, pas besoin de fichier persistence.xml. Le mieux est de signaler un package (le package des classes de persistence, par exemple).

    Je pense que tu trouvera des indications ici:
    https://www.baeldung.com/the-persist...spring-and-jpa

Discussions similaires

  1. Question sur Hibernate
    Par Esil2008 dans le forum Hibernate
    Réponses: 2
    Dernier message: 16/07/2007, 15h11
  2. Question pour Hibernate avec une DataSource
    Par akademiks dans le forum Hibernate
    Réponses: 4
    Dernier message: 28/08/2006, 23h17
  3. Questions sur Hibernate
    Par errant dans le forum Hibernate
    Réponses: 3
    Dernier message: 27/03/2006, 14h18
  4. [Hibernate]Question sur Hibernate
    Par elhani dans le forum Hibernate
    Réponses: 2
    Dernier message: 30/12/2005, 15h39

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