Skip to content

Commit

Permalink
feat: Jakarta Persistence Driver - Support for some delete statements
Browse files Browse the repository at this point in the history
Depends on Eclipselink snapshot version, which contains a few fixes
  • Loading branch information
OndroMih committed Aug 3, 2024
1 parent 12cab75 commit 856926d
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
<!-- impl - data -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>5.0.0-B02</version>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>5.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -159,7 +159,6 @@
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>${jakarta.enterprise.cdi.version}</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@

import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;
import org.eclipse.jnosql.jakartapersistence.mapping.PersistenceDocumentTemplate;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

// selected failing tests that can be worked on next
@Disabled
@EnableAutoWeld
@AddPackages(value = {Converters.class, EntityConverter.class, DocumentTemplate.class})
@AddPackages(DocumentTemplateProducer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@

import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;
import org.eclipse.jnosql.jakartapersistence.mapping.PersistenceDocumentTemplate;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;

@Disabled
//@Disabled
@EnableAutoWeld
@AddPackages(value = {Converters.class, EntityConverter.class, DocumentTemplate.class})
@AddPackages(DocumentTemplateProducer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;
import org.eclipse.jnosql.jakartapersistence.mapping.PersistenceDocumentTemplate;
import org.eclipse.jnosql.jakartapersistence.mapping.spi.JakartaPersistenceExtension;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;

@Disabled
//@Disabled
@EnableAutoWeld
@AddPackages(value = {Converters.class, EntityConverter.class, DocumentTemplate.class})
@AddPackages(DocumentTemplateProducer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<!-- Common properties -->
<property name="jakarta.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDataSource"/>
<!-- In-memory transient DB -->
<property name="jakarta.persistence.jdbc.url" value="jdbc:derby:target/derbydb/test-jpa;create=true"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:derby:memory:test-jpa;create=true"/>
<!-- Embedded DB persisted to filesystem -->
<!--<property name="jakarta.persistence.jdbc.url" value="jdbc:derby:target/derbydb/test-jpa;create=true"/>-->
<property name="jakarta.persistence.jdbc.user" value="APP"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Ondro Mihalyi
*/
package org.eclipse.jnosql.jakartapersistence.mapping;

import jakarta.persistence.EntityManager;
import jakarta.persistence.metamodel.EntityType;
import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;

class BaseQueryParser {

protected final PersistenceDatabaseManager manager;

protected BaseQueryParser(PersistenceDatabaseManager manager) {
this.manager = manager;
}

protected <T> EntityType<T> findEntityType(String entityName) {
return manager.findEntityType(entityName);
}

protected EntityManager entityManager() {
return manager.getEntityManager();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Ondro Mihalyi
*/
package org.eclipse.jnosql.jakartapersistence.mapping;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.metamodel.EntityType;
import jakarta.persistence.metamodel.SingularAttribute;
import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;

class DeleteQueryParser extends BaseQueryParser {


public DeleteQueryParser(PersistenceDatabaseManager manager) {
super(manager);
}

public <T, K> void delete(Class<T> type, K key) {
CriteriaBuilder criteriaBuilder = entityManager().getCriteriaBuilder();
CriteriaDelete<T> deleteCriteria = criteriaBuilder.createCriteriaDelete(type);
Root<?> root = deleteCriteria.from(type);
String entityIdName = getEntityIdName(type);
deleteCriteria.where(criteriaBuilder.equal(root.get(entityIdName), key));
entityManager().createQuery(deleteCriteria).executeUpdate();
}

private <T> String getEntityIdName(Class<T> type) {
EntityType<T> entityType = entityManager().getMetamodel().entity(type);
SingularAttribute<?, ?> idAttribute = entityType.getId(entityType.getIdType().getJavaType());
String entityIdName = idAttribute.getName();
return entityIdName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ public class PersistenceDocumentTemplate implements DocumentTemplate {

private final PersistenceDatabaseManager manager;
private final SelectQueryParser selectParser;
private final DeleteQueryParser deleteParser;

@Inject
PersistenceDocumentTemplate(PersistenceDatabaseManager manager) {
this.manager = manager;
this.selectParser = new SelectQueryParser(manager);
this.deleteParser = new DeleteQueryParser(manager);
}

PersistenceDocumentTemplate() {
manager = null;
selectParser = null;
deleteParser = null;
}

private EntityManager entityManager() {
Expand Down Expand Up @@ -178,8 +181,8 @@ public <T> Iterable<T> update(Iterable<T> itrbl) {
}

@Override
public <T, K> void delete(Class<T> type, K k) {
throw new UnsupportedOperationException("Not supported yet.");
public <T, K> void delete(Class<T> type, K key) {
deleteParser.delete(type, key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.eclipse.jnosql.mapping.PreparedStatement;

Expand Down Expand Up @@ -49,7 +50,13 @@ public PreparedStatement bind(String name, Object value) {

@Override
public <T> Stream<T> result() {
return createQuery().getResultStream();
Query query = createQuery();
try {
return query.getResultStream();
} catch (IllegalStateException e) {
return IntStream.rangeClosed(1, query.executeUpdate())
.mapToObj(i -> (T)Integer.valueOf(i));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,14 @@
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
import org.eclipse.jnosql.jakartapersistence.communication.PersistenceDatabaseManager;

public class SelectQueryParser {

private final PersistenceDatabaseManager manager;
class SelectQueryParser extends BaseQueryParser {

record QueryContext<FROM, RESULT>(CriteriaQuery<RESULT> query, Root<FROM> root, CriteriaBuilder builder) {

}

public SelectQueryParser(PersistenceDatabaseManager manager) {
this.manager = manager;
}

private <T> EntityType<T> findEntityType(String entityName) {
return manager.findEntityType(entityName);
}

private EntityManager entityManager() {
return manager.getEntityManager();
super(manager);
}

public long count(String entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.eclipse.jnosql.jakartapersistence.mapping.repository;

import java.lang.reflect.Method;
import org.eclipse.jnosql.jakartapersistence.mapping.PersistenceDocumentTemplate;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
Expand All @@ -26,4 +27,11 @@ public JakartaPersistenceRepositoryProxy(PersistenceDocumentTemplate template, E
super(template, entities, repositoryType, converters);
}

@Override
protected Object executeCursorPagination(Object instance, Method method, Object[] params) {
// We need to override this because SemiStructuredRepositoryProxy
// expects the semistructured.PreparedStatement template
throw new UnsupportedOperationException("Not supported yet.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#
# Contributors:
#
# Otavio Santana
# Ondro Mihalyi
#
org.eclipse.jnosql.jakartapersistence.mapping.spi.JakartaPersistenceExtension
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ void findByXIn() {
assertThat(persons, hasSize(3));
}

@Test
void hermesParser() {
getEntityManager().createQuery("UPDATE Person SET length = age + 1");
}

private class PersonBuilder {

Person p = new Person();
Expand Down

0 comments on commit 856926d

Please sign in to comment.