我得到了一个org.hibernate.hql.internal.ast.QuerySyntaxException:未映射异常

时间:2016-06-21 10:13:14

标签: java hibernate

我正在尝试从我的表中获取所有对象,但我不明白为什么我会收到此错误...:org.hibernate.hql.internal.ast.QuerySyntaxException:calamartest.personne未映射 我可以插入没有问题,但查询不起作用。

我的代码:

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

我的课程:

@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;

    [...]
}

我的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>

谢谢你的帮助! :)

解决方案

所以我的问题出现在我的查询中:我正在调用postgres标签{myschema}。{mytab}

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

而不是调用我的班级名称:

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

1 个答案:

答案 0 :(得分:4)

错误来自您的查询:from calamartest.personne。没有课程personne,但Personne存在。

还有一件事,确保实体在正确的包装下。在您的代码中,您在from calamartest.Personne内向persistence.xml发出请求,Personne类位于bean包下。