Skip to content

Commit

Permalink
Implement JPA DataStore (#747)
Browse files Browse the repository at this point in the history
* Elide JPA datastore initial commit

* Fix CheckStyle errors

* Passing the bulk of the integration tests for the jpa data store. Fixing a number of bugs with tests and also a few with the data store

* Passing the bulk of the integration tests for the jpa data store. Fixing a number of bugs with tests and also a few with the data store

* Fixed GraphQL Bug

* Resolve conflicts

* Change build order

* Enable UserType test

* Cleanup warnings

* merge master

* Bind entity model beans

* Include missing tests
  • Loading branch information
wcekan authored and aklish committed Apr 7, 2019
1 parent cddb883 commit 418b7f2
Show file tree
Hide file tree
Showing 37 changed files with 1,164 additions and 48 deletions.
6 changes: 3 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* Throw proper exception on invalid PersistentResource where id=null
* Issue#744 Elide returns wrong date parsing format in 400 error for non-default DateFormats

**Features**
* Added [JPA Data Store](https://github.com/yahoo/elide/pull/747)

## 4.3.3
**Fixes**
* Issue#744 Better error handling for mismatched method in Lifecycle and additional test
* Upgraded puppycrawl.tools (checkstyle) dependency to address CVE-2019-9658
* Issue#766 Outdated MySQL driver in elide-standalone and examples

**Features**
* Let custom Elide wrapper process JsonPatchExtensionException

## 4.3.2
**Fixes**
* Issue#754
Expand Down
1 change: 0 additions & 1 deletion elide-datastore/elide-datastore-hibernate3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

<properties>
<dataStoreSupplier>com.yahoo.elide.datastores.hibernate3.HibernateDataStoreSupplier</dataStoreSupplier>
<hibernate3.version>3.6.10.Final</hibernate3.version>
</properties>

<dependencies>
Expand Down
4 changes: 0 additions & 4 deletions elide-datastore/elide-datastore-hibernate5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
<tag>HEAD</tag>
</scm>

<properties>
<hibernate5.version>5.2.15.Final</hibernate5.version>
</properties>

<dependencies>
<!-- Elide (including integration tests) -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.yahoo.elide.datastores.hibernate5;

import com.yahoo.elide.core.DataStore;
import com.yahoo.elide.models.generics.Manager;
import com.yahoo.elide.models.triggers.Invoice;
import com.yahoo.elide.utils.ClassScanner;

Expand Down Expand Up @@ -53,6 +54,7 @@ public DataStore get() {

try {
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class));
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Manager.class.getPackage(), Entity.class));
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Invoice.class.getPackage(), Entity.class));
} catch (MappingException e) {
throw new IllegalStateException(e);
Expand Down
1 change: 1 addition & 0 deletions elide-datastore/elide-datastore-jpa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
191 changes: 191 additions & 0 deletions elide-datastore/elide-datastore-jpa/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<!--
~ Copyright 2016, Yahoo Inc.
~ Licensed under the Apache License, Version 2.0
~ See LICENSE file in project root for terms.
-->

<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>elide-datastore-jpa</artifactId>
<packaging>jar</packaging>
<name>Elide Data Store: JPA</name>
<description>Elide Data Store for JPA support</description>
<url>https://github.com/yahoo/elide</url>
<parent>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-datastore-parent-pom</artifactId>
<version>4.3.4-SNAPSHOT</version>
</parent>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<name>Belov Aleksandr</name>
<url>https://github.com/belovaf</url>
</developer>
</developers>

<scm>
<developerConnection>scm:git:ssh://[email protected]/yahoo/elide.git</developerConnection>
<url>https://github.com/yahoo/elide.git</url>
<tag>HEAD</tag>
</scm>

<properties>
<dataStoreSupplier>com.yahoo.elide.datastores.jpa.JpaDataStoreSupplier</dataStoreSupplier>
</properties>

<dependencies>
<!-- Elide -->
<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-core</artifactId>
</dependency>
<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-datastore-hibernate</artifactId>
<version>4.3.4-SNAPSHOT</version>
</dependency>

<!-- JPA -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>provided</scope>
</dependency>

<!-- JTA (Optional) -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-integration-tests</artifactId>
<version>4.3.4-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>



<!-- Hibernate 5 -->
<!-- Integration test -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate5.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
<exclusion>
<artifactId>jasper-runtime</artifactId>
<groupId>tomcat</groupId>
</exclusion>
<exclusion>
<artifactId>xerces</artifactId>
<groupId>xerces</groupId>
</exclusion>
<exclusion>
<artifactId>jasper-compiler</artifactId>
<groupId>tomcat</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

<!-- Envers testing -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate5.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2018, Oath Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.jpa;

import com.yahoo.elide.core.DataStore;
import com.yahoo.elide.core.DataStoreTransaction;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.datastores.jpa.transaction.JpaTransaction;

import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;

/**
* Implementation for JPA EntityManager data store.
*/
public class JpaDataStore implements DataStore {
protected final EntityManagerSupplier entityManagerSupplier;
protected final JpaTransactionSupplier transactionSupplier;

public JpaDataStore(EntityManagerSupplier entityManagerSupplier,
JpaTransactionSupplier transactionSupplier) {
this.entityManagerSupplier = entityManagerSupplier;
this.transactionSupplier = transactionSupplier;
}

@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
for (EntityType type : entityManagerSupplier.get().getMetamodel().getEntities()) {
try {
Class<?> mappedClass = type.getJavaType();
// Ignore this result. We are just checking to see if it throws an exception meaning that
// provided class was _not_ an entity.
dictionary.lookupEntityClass(mappedClass);

// Bind if successful
dictionary.bindEntity(mappedClass);
} catch (IllegalArgumentException e) {
// Ignore this entity.
// Turns out that JPA may include non-entity types in this list when using things like envers.
// Since they are not entities, we do not want to bind them into the entity dictionary.
}
}
}

@Override
public DataStoreTransaction beginTransaction() {
EntityManager entityManager = entityManagerSupplier.get();
JpaTransaction transaction = transactionSupplier.get(entityManager);
transaction.begin();
return transaction;
}

/**
* Functional interface for describing a method to supply EntityManager.
*/
@FunctionalInterface
public interface EntityManagerSupplier {
EntityManager get();
}

/**
* Functional interface for describing a method to supply JpaTransaction.
*/
@FunctionalInterface
public interface JpaTransactionSupplier {
JpaTransaction get(EntityManager entityManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2018, Oath Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.jpa.porting;

import com.yahoo.elide.core.hibernate.Query;
import com.yahoo.elide.core.hibernate.Session;

import lombok.extern.slf4j.Slf4j;

import javax.persistence.EntityManager;

/**
* Wraps a JPA EntityManager allowing most data store logic
* to not directly depend on a specific version of JPA.
*/
@Slf4j
public class EntityManagerWrapper implements Session {
private EntityManager entityManager;

public EntityManagerWrapper(EntityManager entityManager) {
this.entityManager = entityManager;
}

private static void logQuery(String queryText) {
log.debug("HQL Query: {}", queryText);
}

@Override
public Query createQuery(String queryText) {
logQuery(queryText);
return new QueryWrapper(entityManager.createQuery(queryText));
}
}
Loading

0 comments on commit 418b7f2

Please sign in to comment.