Merge branch 'master' into updateVersion
This commit is contained in:
+6
-1
@@ -19,6 +19,7 @@
|
||||
.idea/
|
||||
*.iml
|
||||
*.iws
|
||||
out/
|
||||
|
||||
# Mac
|
||||
.DS_Store
|
||||
@@ -27,6 +28,9 @@
|
||||
log/
|
||||
target/
|
||||
|
||||
# Gradle
|
||||
.gradle/
|
||||
|
||||
spring-openid/src/main/resources/application.properties
|
||||
.recommenders/
|
||||
/spring-hibernate4/nbproject/
|
||||
@@ -72,4 +76,5 @@ persistence-modules/hibernate5/transaction.log
|
||||
apache-avro/src/main/java/com/baeldung/avro/model/
|
||||
jta/transaction-logs/
|
||||
software-security/sql-injection-samples/derby.log
|
||||
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
||||
spring-soap/src/main/java/com/baeldung/springsoap/gen/
|
||||
/report-*.json
|
||||
@@ -14,7 +14,5 @@
|
||||
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
|
||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
|
||||
|
||||
@@ -8,9 +8,6 @@
|
||||
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
||||
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
||||
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||
@@ -18,3 +15,5 @@
|
||||
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
|
||||
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)
|
||||
- [Convert Latitude and Longitude to a 2D Point in Java](https://www.baeldung.com/java-convert-latitude-longitude)
|
||||
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
@@ -0,0 +1,6 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
@@ -0,0 +1,39 @@
|
||||
<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>
|
||||
<artifactId>algorithms-miscellaneous-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -60,7 +60,6 @@
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<junit.version>4.10</junit.version>
|
||||
<meecrowave-junit.version>1.2.0</meecrowave-junit.version>
|
||||
<okhttp.version>3.10.0</okhttp.version>
|
||||
<meecrowave-jpa.version>1.2.1</meecrowave-jpa.version>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
## OData test URLs
|
||||
|
||||
This following table contains test URLs that can be used with the Olingo V2 demo project.
|
||||
|
||||
| URL | Description |
|
||||
|------------------------------------------|-------------------------------------------------|
|
||||
| `http://localhost:8180/odata/$metadata` | fetch OData metadata document |
|
||||
| `http://localhost:8180/odata/CarMakers?$top=10&$skip=10` | Get 10 entities starting at offset 10 |
|
||||
| `http://localhost:8180/odata/CarMakers?$count` | Return total count of entities in this set |
|
||||
| `http://localhost:8180/odata/CarMakers?$filter=startswith(Name,'B')` | Return entities where the *Name* property starts with 'B' |
|
||||
| `http://localhost:8180/odata/CarModels?$filter=Year eq 2008 and CarMakerDetails/Name eq 'BWM'` | Return *CarModel* entities where the *Name* property of its maker starts with 'B' |
|
||||
| `http://localhost:8180/odata/CarModels(1L)?$expand=CarMakerDetails` | Return the *CarModel* with primary key '1', along with its maker|
|
||||
| `http://localhost:8180/odata/CarModels(1L)?$select=Name,Sku` | Return the *CarModel* with primary key '1', returing only its *Name* and *Sku* properties |
|
||||
| `http://localhost:8180/odata/CarModels?$orderBy=Name asc,Sku desc` | Return *CarModel* entities, ordered by the their *Name* and *Sku* properties |
|
||||
| `http://localhost:8180/odata/CarModels?$format=json` | Return *CarModel* entities, using a JSON representation|
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
HELP.md
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,108 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.baeldung.examples.olingo2</groupId>
|
||||
<artifactId>olingo2-sample</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>olingo2-sample</name>
|
||||
<description>Sample Olingo 2 Project</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<olingo2.version>2.0.11</olingo2.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jersey</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Olingo 2 Dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-core</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
<!-- Avoid jax-rs version conflict by excluding Olingo's version -->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>javax.ws.rs</groupId>
|
||||
<artifactId>javax.ws.rs-api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-api</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-jpa-processor-api</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-jpa-processor-core</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-jpa-processor-ref</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>eclipselink</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
package org.baeldung.examples.olingo2;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityGraph;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.FlushModeType;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.StoredProcedureQuery;
|
||||
import javax.persistence.SynchronizationType;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
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;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ODataJPAServiceFactory implementation for our sample domain
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@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.
|
||||
*/
|
||||
@Override
|
||||
public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
|
||||
|
||||
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(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);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static class EntityManagerWrapper implements EntityManager {
|
||||
|
||||
private EntityManager delegate;
|
||||
|
||||
public void persist(Object entity) {
|
||||
log.info("[I68] persist: entity.class=" + entity.getClass()
|
||||
.getSimpleName());
|
||||
delegate.persist(entity);
|
||||
// delegate.flush();
|
||||
}
|
||||
|
||||
public <T> T merge(T entity) {
|
||||
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());
|
||||
delegate.remove(entity);
|
||||
}
|
||||
|
||||
public <T> T find(Class<T> entityClass, Object primaryKey) {
|
||||
return delegate.find(entityClass, primaryKey);
|
||||
}
|
||||
|
||||
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) {
|
||||
return delegate.find(entityClass, primaryKey, properties);
|
||||
}
|
||||
|
||||
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode) {
|
||||
return delegate.find(entityClass, primaryKey, lockMode);
|
||||
}
|
||||
|
||||
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties) {
|
||||
return delegate.find(entityClass, primaryKey, lockMode, properties);
|
||||
}
|
||||
|
||||
public <T> T getReference(Class<T> entityClass, Object primaryKey) {
|
||||
return delegate.getReference(entityClass, primaryKey);
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
delegate.flush();
|
||||
}
|
||||
|
||||
public void setFlushMode(FlushModeType flushMode) {
|
||||
delegate.setFlushMode(flushMode);
|
||||
}
|
||||
|
||||
public FlushModeType getFlushMode() {
|
||||
return delegate.getFlushMode();
|
||||
}
|
||||
|
||||
public void lock(Object entity, LockModeType lockMode) {
|
||||
delegate.lock(entity, lockMode);
|
||||
}
|
||||
|
||||
public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties) {
|
||||
delegate.lock(entity, lockMode, properties);
|
||||
}
|
||||
|
||||
public void refresh(Object entity) {
|
||||
delegate.refresh(entity);
|
||||
}
|
||||
|
||||
public void refresh(Object entity, Map<String, Object> properties) {
|
||||
delegate.refresh(entity, properties);
|
||||
}
|
||||
|
||||
public void refresh(Object entity, LockModeType lockMode) {
|
||||
delegate.refresh(entity, lockMode);
|
||||
}
|
||||
|
||||
public void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties) {
|
||||
delegate.refresh(entity, lockMode, properties);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
delegate.clear();
|
||||
}
|
||||
|
||||
public void detach(Object entity) {
|
||||
delegate.detach(entity);
|
||||
}
|
||||
|
||||
public boolean contains(Object entity) {
|
||||
return delegate.contains(entity);
|
||||
}
|
||||
|
||||
public LockModeType getLockMode(Object entity) {
|
||||
return delegate.getLockMode(entity);
|
||||
}
|
||||
|
||||
public void setProperty(String propertyName, Object value) {
|
||||
delegate.setProperty(propertyName, value);
|
||||
}
|
||||
|
||||
public Map<String, Object> getProperties() {
|
||||
return delegate.getProperties();
|
||||
}
|
||||
|
||||
public Query createQuery(String qlString) {
|
||||
return delegate.createQuery(qlString);
|
||||
}
|
||||
|
||||
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
|
||||
return delegate.createQuery(criteriaQuery);
|
||||
}
|
||||
|
||||
public Query createQuery(CriteriaUpdate updateQuery) {
|
||||
return delegate.createQuery(updateQuery);
|
||||
}
|
||||
|
||||
public Query createQuery(CriteriaDelete deleteQuery) {
|
||||
return delegate.createQuery(deleteQuery);
|
||||
}
|
||||
|
||||
public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
|
||||
return delegate.createQuery(qlString, resultClass);
|
||||
}
|
||||
|
||||
public Query createNamedQuery(String name) {
|
||||
return delegate.createNamedQuery(name);
|
||||
}
|
||||
|
||||
public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
|
||||
return delegate.createNamedQuery(name, resultClass);
|
||||
}
|
||||
|
||||
public Query createNativeQuery(String sqlString) {
|
||||
return delegate.createNativeQuery(sqlString);
|
||||
}
|
||||
|
||||
public Query createNativeQuery(String sqlString, Class resultClass) {
|
||||
return delegate.createNativeQuery(sqlString, resultClass);
|
||||
}
|
||||
|
||||
public Query createNativeQuery(String sqlString, String resultSetMapping) {
|
||||
return delegate.createNativeQuery(sqlString, resultSetMapping);
|
||||
}
|
||||
|
||||
public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
|
||||
return delegate.createNamedStoredProcedureQuery(name);
|
||||
}
|
||||
|
||||
public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
|
||||
return delegate.createStoredProcedureQuery(procedureName);
|
||||
}
|
||||
|
||||
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
|
||||
return delegate.createStoredProcedureQuery(procedureName, resultClasses);
|
||||
}
|
||||
|
||||
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
|
||||
return delegate.createStoredProcedureQuery(procedureName, resultSetMappings);
|
||||
}
|
||||
|
||||
public void joinTransaction() {
|
||||
delegate.joinTransaction();
|
||||
}
|
||||
|
||||
public boolean isJoinedToTransaction() {
|
||||
return delegate.isJoinedToTransaction();
|
||||
}
|
||||
|
||||
public <T> T unwrap(Class<T> cls) {
|
||||
return delegate.unwrap(cls);
|
||||
}
|
||||
|
||||
public Object getDelegate() {
|
||||
return delegate.getDelegate();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
log.info("[I229] close");
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
boolean isOpen = delegate.isOpen();
|
||||
log.info("[I236] isOpen: " + isOpen);
|
||||
return isOpen;
|
||||
}
|
||||
|
||||
public EntityTransaction getTransaction() {
|
||||
log.info("[I240] getTransaction()");
|
||||
return delegate.getTransaction();
|
||||
}
|
||||
|
||||
public EntityManagerFactory getEntityManagerFactory() {
|
||||
return delegate.getEntityManagerFactory();
|
||||
}
|
||||
|
||||
public CriteriaBuilder getCriteriaBuilder() {
|
||||
return delegate.getCriteriaBuilder();
|
||||
}
|
||||
|
||||
public Metamodel getMetamodel() {
|
||||
return delegate.getMetamodel();
|
||||
}
|
||||
|
||||
public <T> EntityGraph<T> createEntityGraph(Class<T> rootType) {
|
||||
return delegate.createEntityGraph(rootType);
|
||||
}
|
||||
|
||||
public EntityGraph<?> createEntityGraph(String graphName) {
|
||||
return delegate.createEntityGraph(graphName);
|
||||
}
|
||||
|
||||
public EntityGraph<?> getEntityGraph(String graphName) {
|
||||
return delegate.getEntityGraph(graphName);
|
||||
}
|
||||
|
||||
public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass) {
|
||||
return delegate.getEntityGraphs(entityClass);
|
||||
}
|
||||
|
||||
public EntityManagerWrapper(EntityManager delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package org.baeldung.examples.olingo2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
import org.apache.olingo.odata2.core.rest.ODataRootLocator;
|
||||
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Jersey JAX-RS configuration
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@ApplicationPath("/odata")
|
||||
public class JerseyConfig extends ResourceConfig {
|
||||
|
||||
|
||||
public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) {
|
||||
|
||||
ODataApplication app = new ODataApplication();
|
||||
|
||||
app
|
||||
.getClasses()
|
||||
.forEach( c -> {
|
||||
// Avoid using the default RootLocator, as we want
|
||||
// a Spring Managed one
|
||||
if ( !ODataRootLocator.class.isAssignableFrom(c)) {
|
||||
register(c);
|
||||
}
|
||||
});
|
||||
|
||||
register(new CarsRootLocator(serviceFactory));
|
||||
register( new EntityManagerFilter(emf));
|
||||
}
|
||||
|
||||
/**
|
||||
* This filter handles the EntityManager transaction lifecycle.
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) throws IOException {
|
||||
log.info("[I60] >>> filter");
|
||||
EntityManager em = this.emf.createEntityManager();
|
||||
httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE, em);
|
||||
|
||||
// Start a new transaction unless we have a simple GET
|
||||
if (!"GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
em.getTransaction()
|
||||
.begin();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
|
||||
|
||||
log.info("[I68] <<< filter");
|
||||
EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);
|
||||
|
||||
if (!"GET".equalsIgnoreCase(requestContext.getMethod())) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
if (t.isActive()) {
|
||||
if (!t.getRollbackOnly()) {
|
||||
t.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
em.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Path("/")
|
||||
public static class CarsRootLocator extends ODataRootLocator {
|
||||
|
||||
private CarsODataJPAServiceFactory serviceFactory;
|
||||
|
||||
public CarsRootLocator(CarsODataJPAServiceFactory serviceFactory) {
|
||||
this.serviceFactory = serviceFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ODataServiceFactory getServiceFactory() {
|
||||
return this.serviceFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.examples.olingo2;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Olingo2SampleApplication extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Olingo2SampleApplication.class);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.baeldung.examples.olingo2.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name="car_maker")
|
||||
public class CarMaker {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@Column(name="name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy="maker",
|
||||
orphanRemoval = true,
|
||||
cascade=CascadeType.ALL)
|
||||
private List<CarModel> models;
|
||||
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package org.baeldung.examples.olingo2.domain;
|
||||
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name="car_model")
|
||||
public class CarModel {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
private Integer year;
|
||||
|
||||
@NotNull
|
||||
private String sku;
|
||||
|
||||
@ManyToOne(optional=false, fetch= FetchType.LAZY)
|
||||
@JoinColumn(name="maker_fk")
|
||||
private CarMaker maker;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 8180
|
||||
|
||||
spring:
|
||||
jersey:
|
||||
application-path: /odata
|
||||
|
||||
jpa:
|
||||
show-sql: true
|
||||
open-in-view: false
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user