简体   繁体   中英

JPA Entity not mapped exception

I am using JPA and java as technology and writing very simple web application. There is one class called Employee, look like below code

@Entity
@Table(name="Employee")
public class Employee{

    @Id
    private int id;

    @Column(name="name")
    private String name;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

one table called Employee is exist into database. Its persistence.xml file looks like below code.

<?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="JPAService" transaction-type="RESOURCE_LOCAL">
        <!-- Persistence provider -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!-- Entity classes -->
        <class>com.solution.domain.Employee</class>
        <properties>
            <!-- The JDBC driver of your database -->
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <!-- The JDBC URL to the database instance -->
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
            <!-- The database username -->
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <!-- The database password -->
            <property name="javax.persistence.jdbc.password" value="postgres"/>
            <property name="javax.persistence.jdbc.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
         </properties>
    </persistence-unit>
</persistence>

Employee class entry exist in this xml file.

There are not issue while startup of application. Every thing working fine. But when query is executed then it throw and exception.
"Employee is not mapped [select e from Employee e]"

EntityManager  em = getEntityManager();
        em.getTransaction().begin();
        Query query = em.createQuery("select e from Employee e");
    List<Employee> employees = query.getResultList();
        if(employees != null){
            return employees.get(0);
        }

        em.getTransaction().commit();

在此处输入图片说明

I am unable to understand where mapping is missing. As i already mentioned its a web project. So for more clarification i am attaching one screen shot of my project too.

Thanks in advance.

I got the problem. It was an silly mistake. I was using JPA but in Employee class I used the Entity annotation from hibernate implementation instead of JPA package.

Side note: It's a bad idea to model the database with JPA, eg, by implementing the integer surrogate key in the data model. JPA is supposed to help persist an object model, which would use the natural "key" to distinguish objects. A JPA entity should only expose attributes of the object model. This has the benefit of greatly simplifying the expression of relationships because you relate objects, not ID fields. Don't use JPA for database translation, use it for object persistence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM