gpt4 book ai didi

hibernate - 在 hibernate 中将映射类添加为单独的 xml 文件

转载 作者:行者123 更新时间:2023-12-02 23:28:15 25 4
gpt4 key购买 nike

如何将我的 hibernate 映射类作为单独的 xml 文件添加到 cfg 文件中。

这是我的 hibernate 配置文件。

 <hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test

        <property name="connection.username">root</property>
<property name="connection.password">rot@pspl#12</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>

<property name="hbm2ddl.auto">validate</property>

**//how to import mappingclass.xml here **


</session-factory>
</hibernate-configuration>

这里是我的映射类。我需要将这些元素作为mapping.xml 添加到cfg 文件中..

ma​​ppingclass.xml

<mapping class="pepper.logis.item.model.Item" />
<mapping class="pepper.logis.itemstock.model.ItemStock" />
<mapping class="pepper.logis.itemreceiptheader.model.ItemReceiptHeader" />
<mapping class="pepper.logis.itemreceiptdetails.model.ItemReceiptDetails" />
<mapping class="pepper.logis.itemissueheader.model.ItemIssueHeader" />
<mapping class="pepper.logis.itemissuedetails.model.ItemIssueDetails" />
<mapping class="pepper.logis.itemrequisition.model.ItemRequisition" />

谢谢

最佳答案

这可能对你有帮助..

private static SessionFactory buildSessionFactory() {
try {

Configuration configuration = new Configuration();

configuration.configure("hibernate.cfg.xml");

configuration.addAnnotatedClass(pepper.logis.itemstock.model.ItemStock);//add your mapping class here..

ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties());

return configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());

} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

关于hibernate - 在 hibernate 中将映射类添加为单独的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680859/

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