Antoine Grenard
Antoine Grenard

Reputation: 1892

I got an org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped exception

I'm trying to get all the objects from my table but I don't understand why i get this error ... : org.hibernate.hql.internal.ast.QuerySyntaxException: calamartest.personne is not mapped I can insert without a problem but query doesn't work.

My code :

    List<Personne> listPersonne;

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testhibernate0");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();

    listPersonne = entityManager.createQuery("from calamartest.personne", Personne.class).getResultList();

    entityManager.getTransaction().commit();
    entityManager.close();

My classes :

@Entity
@Table( name = "calamartest.personne")
public class Personne {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personne_seq_gen")
    @SequenceGenerator(name = "personne_seq_gen", sequenceName = "calamartest.personne_id_seq",initialValue = 1, allocationSize = 1)
    private int id ;

    private String nom ;

    @OneToMany(mappedBy="personne", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    private List<Maison> listMaison;
    [...]
}

@Entity
@Table( name = "calamartest.maison")
public class Maison {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "maison_seq_gen")
    @SequenceGenerator(name = "maison_seq_gen", sequenceName = "calamartest.maison_id_seq",initialValue = 1, allocationSize = 1)
    private int id;

    @ManyToOne
    private Personne personne;

    private String adresse;

    [...]
}

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="testhibernate0" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>bean.Personne</class>
        <class>bean.Maison</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.id.new_generator_mappings" value="false"/>
            [...]
        </properties>
    </persistence-unit>
</persistence>

Thx for your help ! :)

Solution

So my problem was in my query : i was calling the postgres tab {myschema}.{mytab}

    listPersonne = entityManager.createQuery("from calamartest.personne", Personne.class).getResultList();

Instead of calling my class name :

    listPersonne = entityManager.createQuery("from Personne", Personne.class).getResultList();

Upvotes: 2

Views: 1912

Answers (1)

Dimitri
Dimitri

Reputation: 8290

The error comes from your query : from calamartest.personne. There is no class personne but Personne exists.

One more thing, make sure the entities are under the correct package. In your code, you are making a request to from calamartest.Personne while in the persistence.xml, class Personne is under the bean package.

Upvotes: 4

Related Questions