io.vanillabp.springboot.utils.JpaSpringDataUtil Maven / Gradle / Ivy
package io.vanillabp.springboot.utils;
import io.vanillabp.springboot.adapter.SpringDataUtil;
import jakarta.persistence.Id;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.hibernate.Hibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.data.jpa.repository.JpaContext;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
public class JpaSpringDataUtil implements SpringDataUtil {
private static final Map, JpaRepository, Object>> REPOSITORY_MAP = new HashMap<>();
private static final Map, EntityInformation, Object>> ENTITYINFO_MAP = new HashMap<>();
private final ApplicationContext applicationContext;
private final LocalContainerEntityManagerFactoryBean containerEntityManagerFactoryBean;
private final JpaContext jpaContext;
public JpaSpringDataUtil(
final ApplicationContext applicationContext,
final JpaContext jpaContext,
final LocalContainerEntityManagerFactoryBean containerEntityManagerFactoryBean) {
this.applicationContext = applicationContext;
this.jpaContext = jpaContext;
this.containerEntityManagerFactoryBean = containerEntityManagerFactoryBean;
}
@SuppressWarnings("unchecked")
public JpaRepository super O, Object> getRepository(
final O object) {
//noinspection unchecked
return getRepository((Class) object.getClass());
}
@SuppressWarnings("unchecked")
public JpaRepository getRepository(
final Class type) {
Class super O> cls = type;
if (REPOSITORY_MAP.containsKey(cls)) {
return (JpaRepository) REPOSITORY_MAP.get(cls);
}
var repositories = new Repositories(applicationContext);
Optional