Rakesh Nair Rakesh Nair - 2 months ago 42
Java Question

Object: Employee is not a known Entity type. Eclipselink

I am new to eclipselink or jpa. I was following a tutorial of eclipselink from here. I am getting the following exception when I run the Test.java class. I am using oracle database over network. Please help me.

[EL Info]: connection: 2016-09-14 15:13:33.562--ServerSession(271944754)--/file:/D:/Developer/EclipseLink/bin/_TestPersistence login successful
[EL Warning]: metamodel: 2016-09-14 15:13:33.577--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
Exception in thread "main" java.lang.IllegalArgumentException: Object: Employee [name=Mukesh, email=m@gmail.com , department=Finance] is not a known Entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4226)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:507)
at com.assignment.Test.main(Test.java:28)


persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="TestPersistence"
transaction-type="RESOURCE_LOCAL">
<class>com.assignment.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:oracle:thin:@ipaddress:port/servicename" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password"
value="password" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>


Employee.java

package com.assignment;
public class Employee {
private Long id;
private String name;
private String email;
private String department;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String toString() {
return "Employee [name=" + name + ", email=" + email + " , department="
+ department + "]";
}
}


Test.java

package com.assignment;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class Test {
private static final String PERSISTENCE_UNIT_NAME = "TestPersistence";
private static EntityManagerFactory factory;
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Employee emp = new Employee();
emp.setName("Mukesh");
emp.setEmail("m@gmail.com");
emp.setDepartment("Finance");
em.persist(emp);
em.getTransaction().commit();
em.close();
}
}


Package structure

enter image description here

Answer

Try to use javax.persistence.Entity annotation for Employee class.

More information and examples of EclipseLink usage: https://wiki.eclipse.org/EclipseLink/Examples/JPA