Hibernate / JPA:IllegalArgumentException:不是实体

时间:2022-09-11 11:49:46

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:

我尝试使用hibernate和jpa运行一个基本的应用程序,但现在我在运行应用程序时遇到了这个异常...这是下面的代码和错误:

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:

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.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:

编辑:我忘记粘贴Employee类...所以这里它是:

@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.

如您所见,它已被映射。

1 个解决方案

#1


5  

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

确保实体中的@Entity注释。您还需要在persistence.xml中配置实体

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>pka.EclipseJPAExample.domain.Employee</class>

#1


5  

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

确保实体中的@Entity注释。您还需要在persistence.xml中配置实体

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>pka.EclipseJPAExample.domain.Employee</class>