1

I've got a Java EE Project in my eclipse and i'm using hibernate. But i don't know why when i run my Junit tests i got an unknown entity error.

The thing is that when i run it from my tomcat server calling the method from my servlet everything go fine.

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)

Here the class : package 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;
    [...]
}

The code that is called on ApplicationDao.addApplication(ApplicationDao.java:32) ([...].persist(application)) :

    entityManager = entityManagerFactory.createEntityManager();

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

    entityManager.close();

And my persistence.xml file :

<?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>

I hope you will find what is wrong in my code.

Thx

2
  • 1
    Your class is named com.calamar.beans.Application. But in your persistence.xml, you have <class>beans.Application</class>. Commented Jul 1, 2016 at 6:31
  • @JB Nizet Thx man it was that ! But why does it work when i run the project on a tomcat server ? Commented Jul 1, 2016 at 6:37

1 Answer 1

2

The class element in persistence.xml is distinct from the package of Application class:

Unknown entity: com.calamar.beans.Application

but

<class>beans.Application</class>
Sign up to request clarification or add additional context in comments.

2 Comments

Thx ! It was that ! Same question, why does it work when i run the project on a tomcat server but not when i run the JUnit test from Eclipse ?
From official docs (docs.oracle.com/cd/E16439_01/doc.1013/e13981/cfgdepds005.htm) it is said that it can find annotated entities int the root of PU. The root of the persistence unit is the JAR file or directory, whose META-INF directory contains the persistence.xml file. Suppose for Tomcat your class is picked from root of PT but for Unit tests not (because of the different class loading mechanism)

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.