Hi,
I'm trying to test some DAO objects using TestNG, I'm also using Maven. I have implemented a DAO layer as suggested in Java persistence with Hibernate (16.4.2 Writing DAOs with EJBs).
I created a silly test just to get the things going.
POM.xml
Code:
...
<dependencies>
<dependency>
<groupId>com.eukleia.elf</groupId>
<artifactId>eukleia-entity</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
...
Entity:
Code:
...
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.jboss.seam.annotations.Name;
@Entity
@Table(name="COMPANY")
@EntityListeners(com.eukleia.model.entity.common.monitor.CompanyMonitor.class)
@Name("company")
public class Company implements Serializable{
private static final long serialVersionUID = -7076265969258176592L;
@Id @GeneratedValue
@Column(name="COMPANY_ID")
private Long id;
@Column(name="COMPANY_NAME")
private String name;
@Column(name="COMPANY_FOLDER")
private String folder;
@Column(name="COMPANY_TRIAL")
private boolean trial;
@Temporal(TemporalType.DATE)
@Column(name="COMPANY_EXPIRES")
private Date expires;
@OneToMany(mappedBy = "company", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Division> divisions = new HashSet<Division>();
...
The Test:
Code:
...
public class CompanyDAOTest {
private EntityManagerFactory emf;
private EntityManager em;
private CompanyDAO companyDAO;
@BeforeTest
public void initialise(){
emf = Persistence.createEntityManagerFactory("eukleia-entity-dao-test");
em = emf.createEntityManager();
companyDAO = new CompanyDAOBean();
companyDAO.setEntityManager(em);
}
@AfterTest
public void conclude(){
if (em != null)
em.close();
if (emf != null)
emf.close();
}
@Test
public void loadEukleiaCompanyObject(){
Company company = companyDAO.findById(new Long(1), true);
if (company != null) {
System.out.println("Company name:" + company.getName());
assert true;
} else {
assert false;
}
}
}
...
When I execute: mvn test I get the follow exception:
Code:
java.lang.IllegalArgumentException: Unknown entity: com.eukleia.model.entity.common.Company
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:193)
at com.eukleia.model.dao.GenericEJB3DAO.findById(GenericEJB3DAO.java:38)
at com.eukleia.test.entity.common.CompanyDAOTest.loadEukleiaCompanyObject(CompanyDAOTest.java:40)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.hibernate.MappingException: Unknown entity: com.eukleia.model.entity.common.Company
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:68)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
... 31 more
... Removed 24 stack frames
Click to hide stack frames
java.lang.IllegalArgumentException: Unknown entity: com.eukleia.model.entity.common.Company
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:193)
at com.eukleia.model.dao.GenericEJB3DAO.findById(GenericEJB3DAO.java:38)
at com.eukleia.test.entity.common.CompanyDAOTest.loadEukleiaCompanyObject(CompanyDAOTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:609)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:532)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:686)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1018)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.runWorkers(TestRunner.java:759)
at org.testng.TestRunner.privateRun(TestRunner.java:592)
at org.testng.TestRunner.run(TestRunner.java:486)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
at org.testng.SuiteRunner.run(SuiteRunner.java:204)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:912)
at org.testng.TestNG.runSuitesLocally(TestNG.java:876)
at org.testng.TestNG.run(TestNG.java:784)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.hibernate.MappingException: Unknown entity: com.eukleia.model.entity.common.Company
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:68)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
... 31 more
The entity should be found as it is part of the Maven dependences.
Any thoughts would be appreciated.