0
我使用Hibernate 5.0.7,並且我在註釋配置方面存在問題。休眠5.0.7配置:實體未加載
這是我的hibernate.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=mydb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.domain.Person"/>
</session-factory>
我配置會話工廠以這樣的方式
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
當我嘗試執行上的人查詢我獲得了一個空的列表。我發現類人是不是在會話工廠正確加載(sessionFactory.getAllClassMetadata()返回空列表),並做到這一點的唯一方法是手動添加類:
configuration.addAnnotatedClass(Person.class);
我該如何解決?
完美!解決了!謝謝! – xc93hil
@ cb2783不客氣。請將我的答案標記爲已接受。 –