Skip to content

Instantly share code, notes, and snippets.

@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
@gokepler
gokepler / DefaultQueryDslDeserializer.java
Created October 2, 2024 09:16 — forked from ascott42/DefaultQueryDslDeserializer.java
Querydsl Predicate/OrderSpecifier into JSON
package com.asis.kis.persistence.core.querydsl;
import com.asis.kis.base.commons.stream.SimpleStreams;
import com.asis.kis.commons.util.JsonTypeConverter;
import com.asis.kis.commons.util.Jsons;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import com.querydsl.core.types.EntityPath;
@gokepler
gokepler / Chainer.kt
Created August 24, 2024 03:12 — forked from PatilSiddhesh/Chainer.kt
Kotlin-Function-Chaining-with-Composition
package com.siddroid.folding
fun main() {
// Composing our car
val car = Car.compose(Chain.carEngineType(EngineType.ELECTRIC)
.carOwnerDetails(name = "Android Dev Community",
licenceNo = "API31",
address = "California"
@gokepler
gokepler / 20131212-DSLs-in-action.txt
Created August 24, 2024 02:47 — forked from chenzx/20131212-DSLs-in-action.txt
领域专用语言实战
领域专用语言实战
跳转至: 导航、 搜索
目录
1 初识DSL
2 现实中的DSL
3 DSL驱动的应用程序开发
4 内部DSL实现模式
5 Ruby、Groovy、Clojure语言中的内部DSL设计
6 Scala语言中的内部DSL设计