Initial import
This commit is contained in:
@@ -19,10 +19,13 @@ import javax.persistence.criteria.CriteriaDelete;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.CriteriaUpdate;
|
||||
import javax.persistence.metamodel.Metamodel;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.olingo.odata2.api.processor.ODataContext;
|
||||
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
|
||||
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
|
||||
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
|
||||
import org.baeldung.examples.olingo2.JerseyConfig.EntityManagerFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
||||
@@ -36,15 +39,14 @@ import org.springframework.stereotype.Component;
|
||||
*/
|
||||
@Component
|
||||
public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CarsODataJPAServiceFactory.class);
|
||||
|
||||
|
||||
public CarsODataJPAServiceFactory() {
|
||||
// Enable detailed error messages (useful for debugging)
|
||||
setDetailErrors(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method will be called by Olingo on every request to
|
||||
* initialize the ODataJPAContext that will be used.
|
||||
@@ -54,37 +56,40 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
|
||||
log.info("[I32] >>> initializeODataJPAContext()");
|
||||
ODataJPAContext ctx = getODataJPAContext();
|
||||
|
||||
ODataContext octx = ctx.getODataContext();
|
||||
HttpServletRequest request = (HttpServletRequest)octx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
|
||||
EntityManager em = (EntityManager)request.getAttribute(EntityManagerFilter.EM_REQUEST_ATTRIBUTE);
|
||||
|
||||
// Here we're passing the EM that was created by the EntityManagerFilter (see JerseyConfig)
|
||||
ctx.setEntityManager(new EntityManagerWrapper(EntityManagerHolder.getCurrentEntityManager()));
|
||||
ctx.setEntityManager(new EntityManagerWrapper(em));
|
||||
ctx.setPersistenceUnitName("default");
|
||||
|
||||
// We're managing the EM's lifecycle, so we must inform Olingo that it should not
|
||||
// try to manage transactions and/or persistence sessions
|
||||
ctx.setContainerManaged(true);
|
||||
|
||||
|
||||
ctx.setContainerManaged(true);
|
||||
return ctx;
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class EntityManagerWrapper implements EntityManager {
|
||||
|
||||
|
||||
private EntityManager delegate;
|
||||
|
||||
|
||||
public void persist(Object entity) {
|
||||
log.info("[I68] persist: entity.class=" + entity.getClass().getSimpleName());
|
||||
log.info("[I68] persist: entity.class=" + entity.getClass()
|
||||
.getSimpleName());
|
||||
delegate.persist(entity);
|
||||
//delegate.flush();
|
||||
// delegate.flush();
|
||||
}
|
||||
|
||||
public <T> T merge(T entity) {
|
||||
log.info("[I74] merge: entity.class=" + entity.getClass().getSimpleName());
|
||||
log.info("[I74] merge: entity.class=" + entity.getClass()
|
||||
.getSimpleName());
|
||||
return delegate.merge(entity);
|
||||
}
|
||||
|
||||
public void remove(Object entity) {
|
||||
log.info("[I78] remove: entity.class=" + entity.getClass().getSimpleName());
|
||||
log.info("[I78] remove: entity.class=" + entity.getClass()
|
||||
.getSimpleName());
|
||||
delegate.remove(entity);
|
||||
}
|
||||
|
||||
@@ -287,7 +292,7 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
public EntityManagerWrapper(EntityManager delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package org.baeldung.examples.olingo2;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
public class EntityManagerHolder {
|
||||
|
||||
private static ThreadLocal<EntityManager> currentEntityManager = new ThreadLocal<>();
|
||||
|
||||
|
||||
public static void setCurrentEntityManager(EntityManager em) {
|
||||
currentEntityManager.set(em);
|
||||
}
|
||||
|
||||
public static EntityManager getCurrentEntityManager() {
|
||||
return currentEntityManager.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.examples.olingo2;
|
||||
package org.baeldung.examples.olingo2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -6,12 +6,14 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerRequestFilter;
|
||||
import javax.ws.rs.container.ContainerResponseContext;
|
||||
import javax.ws.rs.container.ContainerResponseFilter;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.olingo.odata2.api.ODataServiceFactory;
|
||||
@@ -57,11 +59,15 @@ public class JerseyConfig extends ResourceConfig {
|
||||
*/
|
||||
@Provider
|
||||
public static class EntityManagerFilter implements ContainerRequestFilter, ContainerResponseFilter {
|
||||
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(EntityManagerFilter.class);
|
||||
|
||||
public static final String EM_REQUEST_ATTRIBUTE = EntityManagerFilter.class.getName() + "_ENTITY_MANAGER";
|
||||
|
||||
private final EntityManagerFactory emf;
|
||||
|
||||
@Context
|
||||
private HttpServletRequest httpRequest;
|
||||
|
||||
public EntityManagerFilter(EntityManagerFactory emf) {
|
||||
this.emf = emf;
|
||||
}
|
||||
@@ -70,11 +76,12 @@ public class JerseyConfig extends ResourceConfig {
|
||||
public void filter(ContainerRequestContext ctx) throws IOException {
|
||||
log.info("[I60] >>> filter");
|
||||
EntityManager em = this.emf.createEntityManager();
|
||||
EntityManagerHolder.setCurrentEntityManager(em);
|
||||
|
||||
httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE, em);
|
||||
|
||||
// Start a new transaction unless we have a simple GET
|
||||
if ( !"GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
em.getTransaction().begin();
|
||||
if (!"GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
em.getTransaction()
|
||||
.begin();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,24 +89,23 @@ public class JerseyConfig extends ResourceConfig {
|
||||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
|
||||
|
||||
log.info("[I68] <<< filter");
|
||||
EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);
|
||||
|
||||
EntityManager em = EntityManagerHolder.getCurrentEntityManager();
|
||||
if ( em != null && !"GET".equalsIgnoreCase(requestContext.getMethod())) {
|
||||
if (!"GET".equalsIgnoreCase(requestContext.getMethod())) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
if ( t.isActive()) {
|
||||
if ( !t.getRollbackOnly()) {
|
||||
if (t.isActive()) {
|
||||
if (!t.getRollbackOnly()) {
|
||||
t.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
em.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Path("/")
|
||||
public static class CarsRootLocator extends ODataRootLocator {
|
||||
|
||||
@@ -113,7 +119,7 @@ public class JerseyConfig extends ResourceConfig {
|
||||
public ODataServiceFactory getServiceFactory() {
|
||||
return this.serviceFactory;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
|
||||
server:
|
||||
port: 8180
|
||||
|
||||
spring:
|
||||
jersey:
|
||||
application-path: /odata
|
||||
|
||||
Reference in New Issue
Block a user