5

I try to run a basic application with hibernate and jpa, but now I'm stuck on this exception when running app...Here's the code and erro below:

java.lang.IllegalArgumentException: Not an entity: class pka.EclipseJPAExample.domain.Employee
at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:158)
at org.hibernate.ejb.criteria.QueryStructure.from(QueryStructure.java:136)
at org.hibernate.ejb.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:177)
at pka.EclipseJPAExample.jpa.JpaTest.createEmployees(JpaTest.java:47)
at pka.EclipseJPAExample.jpa.JpaTest.main(JpaTest.java:33)

JpaTest.java:

public class JpaTest {
private EntityManager manager;
public JpaTest(EntityManager manager) {
    this.manager = manager;
}
/**
 * @param args
 */
public static void main(String[] args) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
    EntityManager manager = factory.createEntityManager();
    JpaTest test = new JpaTest(manager);

    EntityTransaction tx = manager.getTransaction();
    tx.begin();
    try {
        test.createEmployees();
    } catch (Exception e) {
        e.printStackTrace();
    }
    tx.commit();

    test.listEmployees();

    System.out.println(".. done");
}

private void createEmployees() {
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
    query.from(Employee.class);

    int numOfEmployees = manager.createQuery(query).getResultList().size();
    if (numOfEmployees == 0) {
        Department department = new Department("java");
        manager.persist(department);

        manager.persist(new Employee("Jakab Gipsz",department));
        manager.persist(new Employee("Captain Nemo",department));

    }
}


private void listEmployees() {
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
    query.from(Employee.class);
    List<Employee> resultList = manager.createQuery(query).getResultList();

    for (Employee next : resultList) {
        System.out.println("next employee: " + next);
    }
}
}

and persistence.xml:

....<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>

        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testowa" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="enchantsql" />

        <property name="hbm2ddl.auto" value="create" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />        
    </properties>

</persistence-unit>....

Could you point where is the problem?

EDIT: I forgot to paste Employee class...so here it is below:

@Entity
@Table(name="Employee")
public class Employee {
@Id
@GeneratedValue
private Long id;

private String name;

@ManyToOne
private Department department;

public Employee() {}

public Employee(String name, Department department) {
    this.name = name;
    this.department = department;
}


public Employee(String name) {
    this.name = name;
}

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 Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}

@Override
public String toString() {
    return "Employee [id=" + id + ", name=" + name + ", department="
            + department.getName() + "]";
}

}

As you can see it is mapped.

4
  • Where have you mapped your pka.EclipseJPAExample.domain.Employee class? Commented Jul 17, 2013 at 12:08
  • Did you map the Employee class? Via annotation or .hbm.xml? Commented Jul 17, 2013 at 12:08
  • 1
    Try annotating your Employee class with @Entity. I recommend reading through en.wikibooks.org/wiki/Java_Persistence/Mapping Commented Jul 17, 2013 at 12:09
  • 3
    I have already put @Entity annotation in Employee (in my post now you have edited part with Employee class) Commented Jul 18, 2013 at 13:48

2 Answers 2

6

Make sure @Entity annotation in your entity. You also need to configure an entity in persistence.xml

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>pka.EclipseJPAExample.domain.Employee</class>
Sign up to request clarification or add additional context in comments.

5 Comments

You shouldn't need the persistence.xml part, unless you're using an early java ee version.
Now, you are directly running on Java SE Environment.
Actually even so. The annotations are meant so you don't need to write out all the entities in an xml file.
Just curious, how many things from the persistence.xml part above can I replace with annotations ?
I found that it is still necessary to enumerate the entities in persistence.xml. My setup: spring 4.1.1, hibernate-jpa-2.0
0

If you have numerous ENTITY classes in your application, adding an entry for every entity in "persistence.xml" won't be a good choice.

Instead, create your own data source bean using Custom AutoConfiguration.

Use LocalContainerEntityManagerFactoryBean inside dataSource bean Creation method.

Here, you need to define

LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new
LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPackagesToScan("org.springframework.boot.entities");

This package is the location where all entity classes have been saved. Thus no need to define every single entry for Entity classes at "persistence.xml".

Prefer Spring-based Scanning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.