运行junit测试时未知的实体

时间:2016-07-01 06:28:44

标签: java eclipse hibernate junit

我的日食中有一个Java EE项目,我正在使用hibernate。但我不知道为什么当我运行我的Junit测试时,我得到了一个未知的实体错误。

问题是,当我从我的tomcat服务器运行它时,从我的servlet调用方法一切顺利。

java.lang.IllegalArgumentException: Unknown entity: com.calamar.beans.Application
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149)
at com.calamar.dao.ApplicationDao.addApplication(ApplicationDao.java:32)
at com.calamar.services.ApplicationService.addApplication(ApplicationService.java:48)
at com.calamar.test.TestApplicationService.initializeTest(TestApplicationService.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这里的课程:     包com.calamar.beans;

import javax.persistence.*;

@Entity
@Table(name = "calamar.application")
public class Application 
{
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "application_seq_gen")
    @SequenceGenerator(name = "application_seq_gen", sequenceName = "calamar.application_id_seq",initialValue = 1, allocationSize = 1)
    private int id;
    [...]
}

在ApplicationDao.addApplication(ApplicationDao.java:32)([...] .persist(application))上调用的代码:

    entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();
    entityManager.persist(application); 
    entityManager.getTransaction().commit();

    entityManager.close();

我的persistence.xml文件:

<?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="calamar" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>beans.Application</class>
        <class>beans.Derogation</class>
        <class>beans.DerogationAutre</class>
        <class>beans.DerogationFille</class>
        <class>beans.DerogationLinux</class>
        <class>beans.DerogationOracle</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.id.new_generator_mappings" value="false"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.url" value="..."/>
            <property name="hibernate.connection.username" value="..."/>
            <property name="hibernate.connection.password" value="..."/>
        </properties>
    </persistence-unit>
</persistence>

我希望你能找到我的代码中的错误。

THX

1 个答案:

答案 0 :(得分:1)

persistence.xml中的元素与Application类的包不同:

Unknown entity: com.calamar.beans.Application

<class>beans.Application</class>