【问题标题】:Could not locate cfg.xml resource [hibernate.cfg.xml]找不到 cfg.xml 资源 [hibernate.cfg.xml]
【发布时间】:2020-06-22 03:59:30
【问题描述】:

我正在尝试在 Hibernate 中编写一个查询,以使用 where 子句获取给定游戏的所有类型。但是在运行我的应用程序时出现此错误:

Initial SessionFactory creation failed. org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

我知道 XML 映射需要 hibernate.cfg.xml 以便将 POJO 类映射到它们的对应表,但我直接在我的 POJO 类中使用注释来映射它们,而不是 XML。在这种情况下,使用 SessionFactory 似乎是错误的做法,这让我想到了我的问题:使用注释时调用 HQL 查询的正确方法是什么?

谢谢

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String query = "FROM Genre WHERE idGame = " + id;
    
    List genres = (List) session.createQuery(query).list();

【问题讨论】:

  • 需要添加hibernate.cfg.xml吗?

标签: java hibernate


【解决方案1】:

hibernate 配置文件也用于定义不同的 JDBC 连接设置,因此需要创建一个。

你可以像这样创建一个

    <!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>
    
            <!-- JDBC Database connection settings -->
            <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/[DATABASE_NAME]?useSSL=false&amp;serverTimezone=UTC</property>
            <property name="connection.username">[USERNAME]</property>
            <property name="connection.password">[PASSWORD]</property>
    
            <!-- JDBC connection pool settings ... using built-in test pool -->
            <property name="connection.pool_size">1</property>
    
            <!-- Select our SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <!-- Echo the SQL to stdout -->
            <property name="show_sql">true</property>
    
            <!-- Set the current session context -->
            <property name="current_session_context_class">thread</property>
     
        </session-factory>
    
    </hibernate-configuration>

您还需要将它放在项目的根目录中(就像在 src 文件夹中一样)。替换 DATABASE_NAME、USERNAME、PASSWORD 并仔细检查定义的其他属性。

然后你可以像这样构建一个SessionFactory

    SessionFactory factory = new Configuration()
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(Student.class)
                    .buildSessionFactory();

如果 "hibernate.cfg.xml" 参数位于根目录中,您可以从 configure 中删除它(这是 hibernate 查找配置文件的默认路径)。或者您可以手动更改cfg文件的路径,在arg中提及。

然后从SessionFactory得到一个Session

    Session session = factory.getCurrentSession();

【讨论】:

  • application.properties 中已经有了我的数据库连接信息
  • 好吧。您只需要加载属性并创建一个像这样的 SessionFactory factory = new Configuration().setProperties(properties).buildSessionFactory();
【解决方案2】:

我需要添加hibernate.cfg.xml吗? Yes you need a cfg.xml for hibernate 以便它了解其数据库配置。也许你可以参考这个:

Location of hibernate.cfg.xml in project?

【讨论】:

  • application.properties中已经有了我的数据库连接信息,还需要添加吗?
  • 是的,Hibernate 需要 cfg 文件。参考:tutorialspoint.com/hibernate/hibernate_configuration.htm
  • 谢谢!但这不会打开 2 个不同的数据库连接吗?
  • 我怎样才能让他们使用相同的连接?
  • 对于hibernate,你只需要这个cfg.xml,作为一个属性文件,当你使用它时会建立一个连接。我不认为它会打开两个不同的连接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2016-06-14
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多