'How to properly use JPA/Hibernate?

I'm trying to understand annotations better with JPA / Hibernate and SQL Server.

I created a simple project: an abstract class named "Articles". Two classes inherit it: Ramette which adds a weight and Pen which adds a color. The code below is not working and I am unable to correct the errors. Do you have an idea? Thank you!

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 + "]";
    }
 
}



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 + "]";
    }
 
}


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 + "]";
    }
 
}



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



<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns<img src="images/smilies/icon_mad.gif" border="0" alt="" title=":x" class="inlineimg" />si="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="xx" />
            <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>


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


Solution 1:[1]

This makes not much sense. This exception is only thrown when you have a discriminator in the table that has no match in the entity model. Maybe you have trailing spaces in the table?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christian Beikov