1

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

enter image description here

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.

keepmoving
  • 1,813
  • 8
  • 34
  • 74
  • 1
    but it is "listed", so why false for exclude unlisted classes?! – Neil Stockton Jan 09 '16 at 18:40
  • will this help [link](http://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml) – Ravindra Devadiga Jan 09 '16 at 19:02
  • Are you sure the getEntityManager() method is returning you the "JPAService" persistence unit you have defined and not some other persistence unit that might not have that class mapped? – Chris Jan 10 '16 at 05:30
  • @Tine, exclude not working. Giving same exception. – keepmoving Jan 10 '16 at 07:52
  • @Chris, Yes i am sure getEntityManager() method is returning the JPAService persistence unit. – keepmoving Jan 10 '16 at 08:08
  • I also try to run this code as well. But giving same exception. . EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JPAService"); EmployeeDao employeeDao = new EmployeeDao(entityManagerFactory); EntityManager em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); – keepmoving Jan 10 '16 at 08:13

2 Answers2

1

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.

keepmoving
  • 1,813
  • 8
  • 34
  • 74
-1

Side note: It's a bad idea to model the database with JPA, e.g., 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.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10