gpt4 book ai didi

java - 如何配置intellij idea使用hibernate持久化工具并从外部jar文件读取映射文件

转载 作者:行者123 更新时间:2023-12-01 06:04:29 30 4
gpt4 key购买 nike

假设我有下面这个hibernate.cfg.xml包含数据源配置的文件和包含 .hbm.xml 的 jar 文件的路径映射:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.251.134:1521:DB</property>
<property name="hibernate.connection.username">rgh</property>
<property name="hibernate.connection.password">rgh</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.internal_logon">normal</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<property name="hibernate.default_schema">rgh</property>
<property name="hibernate.connection.datasource">jndi</property>
<!-- Mapping files -->

<mapping jar="C:\Users\Ghafari\.m2\repository\org\rgh\goodsCirculation\1.0-releases\goods-1.0-releases.jar"></mapping>
<mapping jar="C:\Users\Ghafari\.m2\repository\org\rgh\core\1.0-releases\core-1.0-releases.jar"></mapping>
<mapping jar="C:\Users\Ghafari\.m2\repository\org\rgh\framework\2.1.1-releases\framework-2.1.1-releases.jar"></mapping>
</session-factory>
</hibernate-configuration>

我应该如何配置intellij idea来使用hibernate控制台并编写hql查询?每个jar文件仅包含.hbm.xml文件

更新

我无法将每个映射设置为 <mapping resource="org/rgh/hibernate/Category.hbm.xml"/> ,因为我有超过 1,500 .hbm.xml文件。

更新

我最近从 Eclipse 迁移到 Intellij Idea,在 Eclipse 中 hql 编辑器工具可以正常使用此 hibernate.cfg.xml 文件,但 Intellij Idea 不起作用。

最佳答案

您可以使用 LocalSessionFactoryBean 的 setMappingJarLocations 方法来加载 jar 位置的类。这是示例:

public static SessionFactory buildSessionFactory(DatabaseData dbData) {
SessionFactory sessionFactory = null;
try {

if (dbData != null) {
// logger.info("dbData not null");
// logger.info("dbData driver : "+dbData.getDataSourceDriver());
// logger.info("dbData IP : "+dbData.getDatabaseIP());
logger.info("dbData DBNAme : " + dbData.getDatabaseSchema());
// logger.info("dbData Uname "+dbData.getDatabaseUserName() +"
// pwd :
// "+dbData.getDatabaseUserName());

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(dbData.getDataSourceDriver());
dataSource.setUrl("jdbc:mysql://" + dbData.getDatabaseIP() + ":" + dbData.getDatabasePort() + "/" + dbData.getDatabaseSchema()
+ "?autoReconnect=true&useSSL=false");
dataSource.setUsername(dbData.getDatabaseUserName());
dataSource.setPassword(dbData.getDatabasePassword());

LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);

Properties hibernateProperties = new Properties();
// hibernateProperties.put("hibernate.dialect",
// HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", false);
//hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
sessionFactoryBean.setHibernateProperties(hibernateProperties);

// load the entity classes from a package
//sessionFactoryBean.setPackagesToScan("com.**.**.entity");
//load the entity classes from jar
sessionFactoryBean.setMappingJarLocations("location for jar file");
sessionFactoryBean.afterPropertiesSet();
return sessionFactoryBean.getObject();
}
} catch (Exception ex) {
logger.error("Initial SessionFactory creation failed.", ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}

DatabaseData 类包含数据库 url、用户名和密码等:

public class DatabaseData {

private String databaseIP;
private String databasePort;
private String databaseName;
private String databaseSchema;
private String databaseUserName;
private String databasePassword;
private String dataSourceDriver;
private int timeout;
}

关于java - 如何配置intellij idea使用hibernate持久化工具并从外部jar文件读取映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47756000/

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