Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.cfg.Configuration;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.context.event.ApplicationReadyEvent;
- import org.springframework.context.ApplicationListener;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
- import org.springframework.data.repository.Repository;
- import org.springframework.stereotype.Component;
- import java.util.Optional;
- import javax.persistence.*;
- @ComponentScan
- public class Main {
- public static void main(String[] args) {
- SpringApplication.run(Main.class, args);
- }
- }
- @Entity
- class Widget {
- @Id
- private Integer id;
- }
- interface WidgetRepository extends Repository<Widget, Integer> {
- Optional<Widget> findById(Integer id);
- }
- @Component
- class Sample implements ApplicationListener<ApplicationReadyEvent> {
- @Override
- public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
- MetadataSources metadataSources = new MetadataSources()
- .addAnnotatedClass(Widget.class);
- Configuration configuration = new Configuration(metadataSources)
- .setProperty("hibernate.connection.url", "jdbc:postgresql://localhost/database");
- EntityManagerFactory entityManagerFactory = configuration.buildSessionFactory();
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- JpaRepositoryFactory repositoryFactory = new JpaRepositoryFactory(entityManager);
- // prints class sample.Widget
- entityManager.getMetamodel().getManagedTypes().forEach(ty -> System.err.println(ty.getJavaType()));
- // throws java.lang.IllegalArgumentException: Not a managed type: class sample.Widget
- entityManager.getMetamodel().managedType(Widget.class);
- // throws java.lang.IllegalArgumentException: Not a managed type: class sample.Widget
- WidgetRepository widgetRepository = repositoryFactory.getRepository(WidgetRepository.class);
- }
- }
Add Comment
Please, Sign In to add comment