java Hibernate/JPA:IllegalArgumentException:不是实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17699312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:43:58  来源:igfitidea点击:

Hibernate/JPA : IllegalArgumentException: Not an entity

javahibernatejpa

提问by Jonaev

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.

如您所见,它已被映射。

回答by Zaw Than oo

Make sure @Entityannotation 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>

回答by Philip Dilip

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

如果您的应用程序中有许多实体类,那么在“persistence.xml”中为每个实体添加一个条目将不是一个好的选择。

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

相反,使用自定义自动配置创建您自己的数据源 bean。

Use LocalContainerEntityManagerFactoryBean inside dataSource bean Creation method.

在 dataSource bean 创建方法中使用 LocalContainerEntityManagerFactoryBean。

Here, you need to define

在这里,您需要定义

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

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

这个包是保存所有实体类的位置。因此无需在“persistence.xml”中定义实体类的每个条目。

Prefer Spring-based Scanning.

首选基于 Spring 的扫描。