com.scalified.jpa.manager.JpaTransactionalManager Maven / Gradle / Ivy
/*
* MIT License
*
* Copyright (c) 2018 Scalified
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package com.scalified.jpa.manager;
import com.scalified.jpa.function.CriteriaFunction;
import com.scalified.jpa.function.ExpressionFunction;
import com.scalified.jpa.function.ResultFunction;
import com.scalified.jpa.sp.SpQuery;
import com.scalified.jpa.specification.Specification;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* A {@link JpaManager} decorator, which adds transaction support
* for entity write operations
*
* @author shell
* @since 2018-02-06
*/
public class JpaTransactionalManager implements JpaManager {
/**
* An underlying {@link JpaManager}
*/
protected final JpaManager manager;
/**
* Creates {@link JpaTransactionalManager} instance
*
* @param manager an {@link JpaManager} to decorate
*/
public JpaTransactionalManager(JpaManager manager) {
this.manager = manager;
}
/**
* Returns an entity found by its {@code primaryKey}
*
* @param entityClass a class of a searched entity
* @param primaryKey a primary key of a searched entity
* @param type of a searched entity
* @param type of a primary key of a searched entity
* @return entity object
*/
@Override
public T find(Class entityClass, K primaryKey) {
return manager.find(entityClass, primaryKey);
}
/**
* Returns the {@link List} of all generic results found by the specified
* {@code entityClass}
*
* @param entityClass a class of a searched entity
* @param type of searched entity
* @return {@link List} of all generic results
*/
@Override
public List find(Class entityClass) {
return manager.find(entityClass);
}
/**
* Returns the generic result found by the specified {@code entityClass}
* and derived from applying the specified {@code resultFunction}
*
* @param entityClass a class of a searched entity
* @param resultFunction a function, which maps {@link CriteriaBuilder}
* to a generic result
* @param type of an entity
* @param type of the result
* @return generic result object
*/
@Override
public R find(Class entityClass, ResultFunction resultFunction) {
return manager.find(entityClass, resultFunction);
}
/**
* Returns the {@link Stream} of generic results found by the specified {@code entityClass}
*
* @param entityClass a class of a searched entity
* @param type of searched entity
* @return {@link Stream} of generic results
*/
@Override
public Stream stream(Class entityClass) {
return manager.stream(entityClass);
}
/**
* Returns the {@link Stream} of generic results found by the specified {@code entityClass},
* which has the specified {@code chunkSize}
*
* @param entityClass a class of a searched entity
* @param chunkSize size of chunk
* @param type of searched entity
* @return {@link Stream} of generic results
*/
@Override
public Stream stream(Class entityClass, int chunkSize) {
return manager.stream(entityClass, chunkSize);
}
/**
* Returns the generic result found by the specified {@code criteriaFunction} and
* derived from applying the specified {@code resultFunction}
*
* @param criteriaFunction a function to find result
* @param resultFunction a function, which maps {@link CriteriaBuilder}
* to a generic result
* @param type of an entity
* @param type of the result
* @return generic result object
*/
@Override
public R find(CriteriaFunction criteriaFunction, ResultFunction resultFunction) {
return manager.find(criteriaFunction, resultFunction);
}
/**
* Returns the {@link Stream} of generic results found by the specified {@code criteriaFunction}
*
* @param criteriaFunction a function to find result
* @param type of an entity
* @return {@link Stream} of generic results
*/
@Override
public Stream stream(CriteriaFunction criteriaFunction) {
return manager.stream(criteriaFunction);
}
/**
* Returns the {@link Stream} of generic results found by the specified {@code criteriaFunction},
* which has the specified {@code chunkSize}
*
* @param criteriaFunction a function to find result
* @param chunkSize size of chunk
* @param type of an entity
* @return {@link Stream} of generic results
*/
@Override
public Stream stream(CriteriaFunction criteriaFunction, int chunkSize) {
return manager.stream(criteriaFunction, chunkSize);
}
/**
* Returns the generic result found by the specified {@code specification} and
* derived from applying the specified {@code resultFunction}
*
* @param specification a specification to find result
* @param resultFunction a function, which maps {@link CriteriaBuilder}
* to a generic result
* @param type of an entity
* @param type of the result
* @return generic result object
*/
@Override
public R find(Specification specification, ResultFunction resultFunction) {
return manager.find(specification, resultFunction);
}
/**
* Returns the raw result as a list containing column values in array of objects produced
* by stored procedure execution built from the specified {@code spQuery}
*
* @param spQuery stored procedure configuration object
* @param type of result
* @return the raw result as a list containing column values in array of objects
*/
@Override
public List © 2015 - 2025 Weber Informatics LLC | Privacy Policy