[英]Set Hibernate mapping values using Java code
我想使用Java代碼設置Hibernate配置:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
.........................
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.web.models.RfidReaderModel"/>
<mapping class="com.web.models.SensorModel"/>
<mapping class="com.web.models.WhiteListModel"/>
<mapping class="com.web.models.BlackListModel"/>
<mapping class="com.web.models.ClientCardModel"/>
<mapping class="com.web.models.SystemUserModel"/>
<mapping class="com.web.models.AuditLogModel"/>
<mapping class="com.web.models.EntranceWFModel"/>
<mapping class="com.web.models.ExitWFModel"/>
<mapping class="com.web.models.RolesPermissions"/>
<mapping class="com.web.models.SystemUserRole"/>
</session-factory>
</hibernate-configuration>
我嘗試了以下代碼實現:
public static SessionFactory buildTestSessionFactory() {
Configuration configuration = new Configuration();
//configuration.configure();
configuration.setProperty("hibernate.show_sql", "true");
configuration.setProperty("hibernate.connection.url", "jdbc:sqlite:/opt/testDB.sqlite");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLiteDialect");
configuration.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
configuration.setProperty("hibernate.connection.release_mode", "auto");
configuration.setProperty("hibernate.connection.autoReconnect", "true");
configuration.setProperty("hibernate.hbm2ddl.auto", "update");
configuration.setProperty("hibernate.current_session_context_class", "thread");
return configuration.buildSessionFactory();
}
但是我還需要設置映射值:
<mapping class="com.web.models.EntranceWFModel"/>
如何使用Java代碼設置它們?
使用Configuration Configuration.addAnnotatedClass(Class annotatedClass)
方法 ,例如:
configuration.addAnnotatedClass(EntranceWFModel.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.