|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository.aot.generated; |
| 17 | + |
| 18 | +import jakarta.persistence.EntityManager; |
| 19 | +import jakarta.persistence.EntityManagerFactory; |
| 20 | +import jakarta.persistence.metamodel.EmbeddableType; |
| 21 | +import jakarta.persistence.metamodel.EntityType; |
| 22 | +import jakarta.persistence.metamodel.ManagedType; |
| 23 | +import jakarta.persistence.metamodel.Metamodel; |
| 24 | +import jakarta.persistence.spi.ClassTransformer; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import org.hibernate.jpa.HibernatePersistenceProvider; |
| 31 | +import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; |
| 32 | +import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor; |
| 33 | +import org.springframework.data.util.Lazy; |
| 34 | +import org.springframework.instrument.classloading.SimpleThrowawayClassLoader; |
| 35 | +import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Christoph Strobl |
| 39 | + */ |
| 40 | +public class AotMetaModel implements Metamodel { |
| 41 | + |
| 42 | + private final String persistenceUnit; |
| 43 | + private final Set<Class<?>> managedTypes; |
| 44 | + private final Lazy<EntityManagerFactory> entityManagerFactory = Lazy.of(this::init); |
| 45 | + private final Lazy<Metamodel> metamodel = Lazy.of(() -> entityManagerFactory.get().getMetamodel()); |
| 46 | + private final Lazy<EntityManager> entityManager = Lazy.of(() -> entityManagerFactory.get().createEntityManager()); |
| 47 | + |
| 48 | + public AotMetaModel(Set<Class<?>> managedTypes) { |
| 49 | + this("dynamic-tests", managedTypes); |
| 50 | + } |
| 51 | + |
| 52 | + private AotMetaModel(String persistenceUnit, Set<Class<?>> managedTypes) { |
| 53 | + this.persistenceUnit = persistenceUnit; |
| 54 | + this.managedTypes = managedTypes; |
| 55 | + } |
| 56 | + |
| 57 | + public static AotMetaModel hibernateModel(Class<?>... types) { |
| 58 | + return new AotMetaModel(Set.of(types)); |
| 59 | + } |
| 60 | + |
| 61 | + public static AotMetaModel hibernateModel(String persistenceUnit, Class<?>... types) { |
| 62 | + return new AotMetaModel(persistenceUnit, Set.of(types)); |
| 63 | + } |
| 64 | + |
| 65 | + public <X> EntityType<X> entity(Class<X> cls) { |
| 66 | + return metamodel.get().entity(cls); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public EntityType<?> entity(String s) { |
| 71 | + return metamodel.get().entity(s); |
| 72 | + } |
| 73 | + |
| 74 | + public <X> ManagedType<X> managedType(Class<X> cls) { |
| 75 | + return metamodel.get().managedType(cls); |
| 76 | + } |
| 77 | + |
| 78 | + public <X> EmbeddableType<X> embeddable(Class<X> cls) { |
| 79 | + return metamodel.get().embeddable(cls); |
| 80 | + } |
| 81 | + |
| 82 | + public Set<ManagedType<?>> getManagedTypes() { |
| 83 | + return metamodel.get().getManagedTypes(); |
| 84 | + } |
| 85 | + |
| 86 | + public Set<EntityType<?>> getEntities() { |
| 87 | + return metamodel.get().getEntities(); |
| 88 | + } |
| 89 | + |
| 90 | + public Set<EmbeddableType<?>> getEmbeddables() { |
| 91 | + return metamodel.get().getEmbeddables(); |
| 92 | + } |
| 93 | + |
| 94 | + public EntityManager entityManager() { |
| 95 | + return entityManager.get(); |
| 96 | + } |
| 97 | + |
| 98 | + EntityManagerFactory init() { |
| 99 | + |
| 100 | + MutablePersistenceUnitInfo persistenceUnitInfo = new MutablePersistenceUnitInfo() { |
| 101 | + @Override |
| 102 | + public ClassLoader getNewTempClassLoader() { |
| 103 | + return new SimpleThrowawayClassLoader(this.getClass().getClassLoader()); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void addTransformer(ClassTransformer classTransformer) { |
| 108 | + // just ingnore it |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + persistenceUnitInfo.setPersistenceUnitName(persistenceUnit); |
| 113 | + this.managedTypes.stream().map(Class::getName).forEach(persistenceUnitInfo::addManagedClassName); |
| 114 | + |
| 115 | + persistenceUnitInfo.setPersistenceProviderClassName(HibernatePersistenceProvider.class.getName()); |
| 116 | + |
| 117 | + return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo) { |
| 118 | + @Override |
| 119 | + public List<String> getManagedClassNames() { |
| 120 | + return persistenceUnitInfo.getManagedClassNames(); |
| 121 | + } |
| 122 | + }, Map.of("hibernate.dialect", "org.hibernate.dialect.H2Dialect")).build(); |
| 123 | + } |
| 124 | +} |
0 commit comments