Maruli Maruli - 7 months ago 8
Java Question

Inject DAO into Restlet Resource class

I have this class with a method that returns String:

package com.fwd.pmap.service.dao;

import javax.persistence.EntityManager;

import com.fwd.pmap.util.EntityManagerUtil;

public class PMAPMatrixDAOImpl implements PMAPMatrixDAO
{
private EntityManager entityManager = EntityManagerUtil.getEntityManager();

public String findPMAP(int campaignID, String staffCategory, int ispPlanID)
{
String pmapPlan = "";

// If any of the mandatory fields is empty, return empty string
if(campaignID == 0 || staffCategory.isEmpty() || ispPlanID == 0)
return "Make sure all parameters are supplied";

String sqlQuery = "SELECT P.PlanName FROM ClientPMAP P, PMAPMatrix M "
+ "WHERE P.ID = M.ClientPMAPID AND "
+ "M.campaign_id = :campaignID AND "
+ "M.StaffCategoryID = (SELECT ID FROM StaffCategory WHERE Name = :staffCat) AND "
+ "M.isp_plan_id = :planID AND "
+ "M.PreEx IS NULL";

pmapPlan = (String) entityManager.createNativeQuery(sqlQuery)
.setParameter("campaignID", campaignID)
.setParameter("staffCat", staffCategory)
.setParameter("planID", ispPlanID)
.getSingleResult();

return pmapPlan;
}
}


Application

package com.fwd.pmap.service.web;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class EmployerApplication extends Application
{
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {

Router router = new Router(getContext());

//http://localhost:9090/fwd-PMAP/pmap/campaign/1/staffCat/EXA1(A)/isp/10
router.attach("/pmap/campaign/{campaign}/staffCat/{staffCat}/isp/{isp}", EmployerResource.class);

return router;
}
}


Resource

package com.fwd.pmap.service.web;

import org.restlet.data.Reference;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.fwd.pmap.service.dao.PMAPMatrixDAO;
import com.fwd.pmap.service.dao.PMAPMatrixDAOImpl;

public class EmployerResource extends ServerResource
{
PMAPMatrixDAO matrix = new PMAPMatrixDAOImpl();

@Get
public String represent()
{
String campaignID = (String) this.getRequestAttributes().get("campaign");
String ispID = (String) this.getRequestAttributes().get("isp");
String staffCat = Reference.decode((String) this.getRequestAttributes().get("staffCat"));

String tmp = "Campaign=[" + campaignID + "], Staff Cat=[" + staffCat + "], ISP=[" + ispID + "]";

matrix.findPMAP(Integer.parseInt(campaignID), staffCat, Integer.parseInt(ispID));

return tmp;
}
}


If I don't include PMAPMatrixDAO classes in my resource file the program works fine and return the string as expect. However, if i include the DAO I got an error below:

13:16:39,987 WARN [org.jboss.modules] (http-localhost/127.0.0.1:9090-1) Failed to define class org.hibernate.engine.spi.CascadeStyles$BaseCascadeStyle in Module "deployment.fwd-PMAP.war:main" from Service Module Loader: java.lang.IncompatibleClassChangeError: Implementing class
at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.8.0_73]
at java.lang.ClassLoader.defineClass(Unknown Source) [rt.jar:1.8.0_73]
at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:361) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:482) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:277) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:92) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.Module.loadModuleClass(Module.java:568) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134) [jboss-modules.jar:1.3.6.Final-redhat-1]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildBootstrapServiceRegistry(EntityManagerFactoryBuilderImpl.java:484) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:206) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:182) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:131) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:88) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:69) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) [hibernate-jpa-2.0-api-1.0.1.Final-redhat-3.jar:1.0.1.Final-redhat-3]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final-redhat-3.jar:1.0.1.Final-redhat-3]
at com.fwd.pmap.util.EntityManagerUtil.<clinit>(EntityManagerUtil.java:19) [classes:]
at com.fwd.pmap.service.dao.PMAPMatrixDAOImpl.<init>(PMAPMatrixDAOImpl.java:9) [classes:]
at com.fwd.pmap.service.web.EmployerResource.<init>(EmployerResource.java:12) [classes:]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.8.0_73]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.8.0_73]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.8.0_73]
at java.lang.reflect.Constructor.newInstance(Unknown Source) [rt.jar:1.8.0_73]
at java.lang.Class.newInstance(Unknown Source) [rt.jar:1.8.0_73]
at org.restlet.resource.Finder.create(Finder.java:148) [org.restlet-2.3.4.jar:]
at org.restlet.resource.Finder.create(Finder.java:176) [org.restlet-2.3.4.jar:]
at org.restlet.resource.Finder.find(Finder.java:193) [org.restlet-2.3.4.jar:]
at org.restlet.resource.Finder.handle(Finder.java:219) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.handle(Router.java:639) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202) [org.restlet-2.3.4.jar:]
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:75) [org.restlet-2.3.4.jar:]
at org.restlet.Application.handle(Application.java:385) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.handle(Router.java:639) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.doHandle(Router.java:422) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Router.handle(Router.java:639) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.doHandle(Filter.java:150) [org.restlet-2.3.4.jar:]
at org.restlet.routing.Filter.handle(Filter.java:197) [org.restlet-2.3.4.jar:]
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202) [org.restlet-2.3.4.jar:]
at org.restlet.Component.handle(Component.java:408) [org.restlet-2.3.4.jar:]
at org.restlet.Server.handle(Server.java:507) [org.restlet-2.3.4.jar:]
at org.restlet.engine.connector.ServerHelper.handle(ServerHelper.java:63) [org.restlet-2.3.4.jar:]
at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:143) [org.restlet-2.3.4.jar:]
at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1117) [org.restlet.ext.servlet-2.3.4.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-2.jar:1.0.2.Final-redhat-2]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:150) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:854) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.5.7.Final-redhat-1.jar:7.5.7.Final-redhat-1]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_73]


pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fwd</groupId>
<artifactId>fwd-PMAP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.8</java.version>

<!-- Spring -->
<spring-framework.version>4.0.3.RELEASE</spring-framework.version>

<!-- Hibernate / JPA -->
<hibernate.version>4.3.5.Final</hibernate.version>

<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<log4j.version>1.2.16</log4j.version>

<restlet.version>2.3.4</restlet.version>
<jackson.version>2.7.1</jackson.version>
<groovy.version>2.4.5</groovy.version>
</properties>

<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<!-- Spring ORM support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- 3cp0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- CSV file reader utilities -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</version>
</dependency>

<!-- Copy POJO to POJO -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.5</version>
</dependency>

<!-- Restlet -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>


How should I access the DAO class within a webservice?

Answer

After agonizing for 3 over weeks I finally figured it out, it's caused by version incompatibility!!!

EAP 6.4 is NOT compatible with JPA 2.1 so I have to downgrade. Below are the steps I've taken to resolve this:

  1. Right click on Project > Properties > JPA > Change JPA Version (Select 2.0)
  2. Downgrade Hibernate to <hibernate.version>4.2.21.Final</hibernate.version> in the POM file
  3. Change the top of persistence file to version 2.0
<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">
  1. Run maven clean install