白季飞龙的个人主页

Hibernate大杂烩

SpringBoot集成Hibernate

App.java

package bj.demo;

import org.hibernate.SessionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import java.util.List;

@SpringBootApplication
public class App implements ApplicationListener<ApplicationReadyEvent> {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Resource
    private
    EntityManagerFactory entityManagerFactory;

    @Override
    public void onApplicationEvent(@Nonnull ApplicationReadyEvent applicationReadyEvent) {
        SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
        List<Animal> animals = sessionFactory.openSession().createQuery("FROM bj.demo.Animal", Animal.class).list();
        System.out.println(animals);
    }
}

文章首发: https://baijifeilong.github.io


漫漫路,莫论逍遥;潜心修,只为悟道