我正在尝试从表中获取所有对象,但我不明白为什么会收到此错误...: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();

最佳答案

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

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

关于java - 我有一个org.hibernate.hql.internal.ast.QuerySyntaxException:未映射异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37941615/

10-09 05:25