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);
}
}