Java 找不到cfg.xml资源[hibernate.cfg.xml]

Java 找不到cfg.xml资源[hibernate.cfg.xml],java,hibernate,Java,Hibernate,我试图在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

我试图在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吗<代码>是您需要一个用于hibernate的cfg.xml以便它了解其数据库配置。您可以参考以下内容:


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>
    SessionFactory factory = new Configuration()
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(Student.class)
                    .buildSessionFactory();
如果“hibernate.cfg.xml”参数位于根目录中,则可以从configure中删除它(这是hibernate查找配置文件的默认路径)。或者您可以手动更改cfg文件的路径,在arg中提及它

然后从
SessionFactory

    Session session = factory.getCurrentSession();

我需要添加hibernate.cfg.xml吗?我在application.propertiesOkay中已经有了数据库连接信息。您只需要加载属性并创建一个SessionFactory,如SessionFactory=new Configuration().setProperties(properties.buildSessionFactory();我在application.properties中已经有了数据库连接信息,是否还需要添加它?对于Hibernate,需要cfg文件。参考:谢谢!但是这不会打开两个不同的数据库连接吗?我如何才能让它们使用相同的连接?对于hibernate,您只需要这个cfg.xml,作为属性文件,使用它时将建立连接。我不认为它会打开两个不同的连接