Skip to content

Instantly share code, notes, and snippets.

@gokepler
gokepler / ExampleApp.java
Created September 24, 2025 11:14 — forked from thomasdarimont/ExampleApp.java
Example PoC for class based Projections with Spring Data Repositories
package demo;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
@gokepler
gokepler / AspectJProxyFactorySample.java
Created August 8, 2025 13:45 — forked from rponte/AspectJProxyFactorySample.java
Creating Spring AspectJ Proxy programmatically
XuxuService service = new XuxuServiceImpl();
RetryAndNotifyOnErrorAspect myaspect = new RetryAndNotifyOnErrorAspect();
AspectJProxyFactory factory = new AspectJProxyFactory(service);
factory.addAspect(myaspect);
// factory.setProxyTargetClass(true); // if you're applying to a concrete class
XuxuService proxy = factory.getProxy();
// just run it
proxy.execute();
USE AdventureWorks2008R2
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('Purchasing.usp_AT_uPurchaseOrderDetail', 'P') IS NOT NULL
DROP PROCEDURE Purchasing.usp_AT_uPurchaseOrderDetail;
GO
CREATE PROCEDURE Purchasing.usp_AT_uPurchaseOrderDetail
@gokepler
gokepler / CustomizedArgumentParser
Created December 1, 2024 10:04 — forked from cherniag/CustomizedArgumentParser
CustomizedPredicateBuilderStrategy for RSQL JPA, processes defined custom RSQL operator (=containsPair= in this case) and transforms to JPQL predicate to query by map's key and value
import com.github.tennaito.rsql.misc.ArgumentFormatException;
import com.github.tennaito.rsql.misc.DefaultArgumentParser;
import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
// default DefaultArgumentParser can't parse Timestamps :(
public class CustomizedArgumentParser extends DefaultArgumentParser {
@gokepler
gokepler / jpaToHql.java
Created December 1, 2024 07:20 — forked from Crydust/jpaToHql.java
convert jpa TypedQuery to hibernate hql (=jpql)
// @see http://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/
TypedQuery<X> q = entityManager.createQuery(cq);
try {
Class<?> hibernateQueryClass = Class.forName("org.hibernate.Query");
Object hibernateQuery = q.unwrap(hibernateQueryClass);
java.lang.reflect.Method getQueryStringMethod = hibernateQueryClass.getMethod("getQueryString");
Object hql = getQueryStringMethod.invoke(hibernateQuery);
LOGGER.warn("hql = {}", hql);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | java.lang.reflect.InvocationTargetException ex) {
ex.printStackTrace();
@gokepler
gokepler / RepositoryImpl.java
Created December 1, 2024 04:48 — forked from koraktor/RepositoryImpl.java
Combining specifications and projections in Spring Data JPA
public class RepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID extends Serializable> {
ProjectionFactory projectionFactory;
public <P> List<P> findProjected(Specification<?> spec, Sort sort, Class<P> projectionClass) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> tupleQuery = criteriaBuilder.createTupleQuery();
Root<?> root = tupleQuery.from(getDomainClass());
@gokepler
gokepler / DeviceDataRepository.java
Created November 30, 2024 12:57 — forked from brunnels/DeviceDataRepository.java
Generic REST Query Language with RSQL for Spring Data JPA
package org.kraven.repository;
import org.kraven.domain.DeviceData;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* Spring Data JPA repository for the DeviceData entity.
*/
@SuppressWarnings("unused")
public interface DeviceDataRepository extends JpaRepository<DeviceData,Long>, JpaSpecificationExecutor<DeviceData> {
@gokepler
gokepler / ProxyTest.java
Created November 26, 2024 13:24 — forked from nathansgreen/ProxyTest.java
java.lang.reflect.Proxy over Annotation
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
@gokepler
gokepler / JpaEntityQueryBuilder.java
Created November 25, 2024 12:27 — forked from ufuk/JpaEntityQueryBuilder.java
Easy to use query builder for JPA Criteria API
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.criteria.internal.path.PluralAttributePath;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
@gokepler
gokepler / HOWTO.md
Created November 17, 2024 05:56 — forked from Toparvion/HOWTO.md
JUnit test for conditional Spring Boot bean registration (@ConditionalOnProperty)

Problem

Suppose you have two classes that should be registered with Spring context exclusively, e.g. only one of the beans must exist in the context at any time based on some boolean property value. Of course you can do it by adding explicit if condition into your @Configuration class. But if the classes have no common interface it may be quite cumbersome. As an alternative you can use @ConditionalOnProperty annotation on your classes, e.g.:

@Service
@ConditionalOnProperty(name = "use-left-service", havingValue = "true", matchIfMissing = false)
public class LeftService