gpt4 book ai didi

java - 使用 Java 代码设置 Hibernate 映射值

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:18 25 4
gpt4 key购买 nike

我想使用 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) method ,例如:

configuration.addAnnotatedClass(EntranceWFModel.class);

关于java - 使用 Java 代码设置 Hibernate 映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com