Spring Boot 应用程序中的 application.properties 与 hibernate.cfg.xml

Muh*_*mad 6 hibernate spring-boot

我已经在application.propertiesSpring Boot 应用程序的文件中配置了休眠属性。

应用程序属性

#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.url=<db_url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
# Show or not log for each sql query
spring.jpa.show-sql = true
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy     

# ThymeLeaf
spring.thymeleaf.cache= false
spring.thymeleaf.mode=LEGACYHTML5   
Run Code Online (Sandbox Code Playgroud)

当我尝试获取会话时出现错误

Configuration configuration = new Configuration();
configuration.configure("application.properties");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build());
Session session = sessionFactory.openSession();
Run Code Online (Sandbox Code Playgroud)

错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: 
Could not parse configuration: application.properties] with root cause

org.dom4j.DocumentException: Error on line 1 of document  : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
Run Code Online (Sandbox Code Playgroud)

hibernate.cfg.xml我认为它也在类路径中期待文件?

有什么方法我只能使用application.properties或者必须将所有与休眠相关的属性移动到hibernate.cfg.xmlhibernate.properties文件?

获取选定的学生

public List getSelectedStudents(){
    final EntityManagerFactory emf = null;
    EntityManager em = emf.createEntityManager();

    Query q = em.createNativeQuery("SELECT s.student_id, s.first_name, s.last_name, s.city FROM Student s "
                                    + "where s.city=:city and s.last_name = :lname", Student.class);
    q.setParameter("city", "London");
    q.setParameter("lname", "Rizwan");

    List<Student> students = q.getResultList();

    for (Student s : students) {
        System.out.println("Student "
                + s.getFirstName()
                + " "
                + s.getLastName());
    }

    return students;
}   
Run Code Online (Sandbox Code Playgroud)

错误2:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.school.service.StudentServiceImplementation.getSelectedStudents(StudentServiceImplementation.java:69) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77] 
Run Code Online (Sandbox Code Playgroud)

编辑:按照建议使用实体管理器,我添加了 getSelectedStudents 方法。我仍然收到错误,请EntityManager em = emf.createEntityManager(); 参阅错误 2 了解详细信息。

Nar*_*ros 2

如果您将 spring-boot 与 spring-autoconfigure 结合使用,您只需将 Hibernate 库放到您的类路径中,spring 就会自动为您连接 Hibernate。您需要做的就是在 application.properties 文件中提供一些 spring jpa 配置设置,然后就完成了。

如果您想避免使用 spring-autoconfigure,那么您需要添加一个配置类来构造 a LocalContainerEntityManagerFactoryBean、 aJpaTransactionManager和您的DataSource.

无论哪种情况,要在应用程序中使用 JPA,您只需将带注释的属性添加到存储库或服务类即可获取实例,如下所示EntityManager

@PersistentContext
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)

Spring 将确保为您注入它,并且您的代码只需要根据需要使用它即可。