Java 休眠错误:无法解析表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/970573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:44:20  来源:igfitidea点击:

Hibernate error: cannot resolve table

javahibernateintellij-ideamapping

提问by Roman

I'm trying to make work the example from hibernate reference.

我正在尝试使用 hibernate 参考中的示例。

I've got simple table Pupil with id, name and age fields. I've created correct (as I think) java-class for it according to all java-beans rules.

我有一个简单的表 Pupil,其中包含 id、name 和 age 字段。我已经根据所有 java-beans 规则为它创建了正确的(正如我认为的)java-class。

I've created configuration file - hibernate.cfg.xml, just like in the example from reference.

我已经创建了配置文件 - hibernate.cfg.xml,就像参考中的示例一样。

I've created hibernate mapping for one class Pupil, and here is the error occured.

我已经为一类学生创建了休眠映射,这是发生的错误。

<hibernate-mapping>
   <class name="Pupil" table="pupils">
       ...
   </class>
</hibernate-mapping>

table="pupils" is red in my IDE and I see message "cannot resolve table pupils". I've also founded very strange note in reference which says that most users fail with the same problem trying to run the example.

table="pupils" 在我的 IDE 中是红色的,我看到消息“无法解析表格学生”。我还在参考文献中创建了一个非常奇怪的注释,它说大多数用户在尝试运行该示例时都遇到了同样的问题。

Ah.. I'm very angry with this example.. IMHO if authors know that there is such problem they should add some information about it.

啊..我对这个例子很生气..恕我直言,如果作者知道存在这样的问题,他们应该添加一些关于它的信息。

But, how should I fix it? I don't want to deal with Ant here and with other instruments used in example. I'm using MySql 5.0, but I think it doesn't matter.

但是,我应该如何修复它?我不想在这里处理 Ant 以及示例中使用的其他工具。我正在使用 MySql 5.0,但我认为这无关紧要。

UPD: source code

UPD:源代码

Pupil.java - my persistent class

Pupil.java - 我的持久类

package domain;

public class Pupil {
    private Integer id;
    private String name;
    private Integer age;

    protected Pupil () { }

    public Pupil (String name, int age) {
        this.age = age;
        this.name = name;
    }

    public Integer getId () {
        return id;
    }

    public void setId (Integer id) {
        this.id = id;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public Integer getAge () {
        return age;
    }

    public void setAge (Integer age) {
        this.age = age;
    }

    public String toString () {
        return "Pupil [ name = " + name + ", age = " + age + " ]";
    }
}

Pupil.hbm.xml is mapping for this class

Pupil.hbm.xml 是这个类的映射

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="domain" >

    <class name="Pupil" table="pupils">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name" not-null="true"/>
        <property name="age"/>
    </class>
</hibernate-mapping>

hibernate.cfg.xml - configuration for hibernate

hibernate.cfg.xml - 休眠配置

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

        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>

        <mapping resource="domain/Pupil.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

HibernateUtils.java

HibernateUtils.java

package utils;

import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration ().configure ().buildSessionFactory ();
        } catch (HibernateException he) {
            System.err.println (he);
            throw new ExceptionInInitializerError (he);
        }
    }

    public static SessionFactory getSessionFactory () {
        return sessionFactory;
    }
}

Runner.java - class for testing hibernate

Runner.java - 用于测试休眠的类

import org.hibernate.Session;

import java.util.*;

import utils.HibernateUtils;
import domain.Pupil;

public class Runner {
    public static void main (String[] args) {
        Session s = HibernateUtils.getSessionFactory ().getCurrentSession ();

        s.beginTransaction ();
        List pups = s.createQuery ("from Pupil").list ();
        for (Object obj : pups) {
            System.out.println (obj);
        }
        s.getTransaction ().commit ();

        HibernateUtils.getSessionFactory ().close ();
    }
}

My libs: antlr-2.7.6.jar, asm.jar, asm-attrs.jar, cglib-2.1.3.jar, commons-collections-2.1.1.jar, commons-logging-1.0.4.jar, dom4j-1.6.1.jar, hibernate3.jar, jta.jar, log4j-1.2.11.jar, mysql-connector-java-5.1.7-bin.jar

我的库:antlr-2.7.6.jar、asm.jar、asm-attrs.jar、cglib-2.1.3.jar、commons-collections-2.1.1.jar、commons-logging-1.0.4.jar、dom4j -1.6.1.jar、hibernate3.jar、jta.jar、log4j-1.2.11.jar、mysql-connector-java-5.1.7-bin.jar

Compile error: cannot resolve table pupils

编译错误:无法解析表学生

采纳答案by Pascal Thivent

This has nothing to do with Hibernate, this is an IDEA "issue" and you need to configure it properly for tables names validation in hbm.xml. From this old thread:

这与 Hibernate 无关,这是一个 IDEA“问题”,您需要为 hbm.xml 中的表名称验证正确配置它。从这个旧线程

In order for IntelliJ to provide proper code completion and validation for database tables/columns, it needs to know about the database structure of your application as well. So, I'm referring to the IntelliJ datasource. Think of it as a "development-time datasource", or something like that.

To create one:
Window -> Tool Windows -> Data sources
Add ("plus" icon) -> JDBC data source

As an alternative, you could try the "Import" button in the "Date sources" tool window. This makes IntelliJ search your project for some specific configuration files (like "hibernate.cfg.xml"), from which it can directly import a datasource definition.

However, if that fails, you can always define a JDBC data source manually (jdbc url, driver jar, driver class, etc).

Once you have a datasource configured, test it by opening an SQL console on it ("console" button in datasource tool window), and type some queries. IDEA should provide SQL code completion here, for table and column names.

If this step works, go to the definition of the datasource, and invoke

"Refresh Tables". This makes IntelliJ retrieve the database structure.

Next, open "Project Structure" (Ctrl-Shift-Alt-S). Select your Hibernate facet (though either "Facets" or "Modules").

The options screen for the Hibernate facet has a pane named "DataSources Mapping". Here you can associate your Hiberante session factory with a specific IntelliJ datasource.

After this step, SQL table/column code completion and validation should work in .hbm files as well.

为了让 IntelliJ 为数据库表/列提供正确的代码完成和验证,它还需要了解应用程序的数据库结构。所以,我指的是 IntelliJ 数据源。将其视为“开发时数据源”或类似的东西。

创建一个:
窗口->工具窗口->数据源
添加(“加号”图标)->JDBC数据源

作为替代方法,您可以尝试使用“日期源”工具窗口中的“导入”按钮。这使得 IntelliJ 在您的项目中搜索一些特定的配置文件(如“hibernate.cfg.xml”),它可以从中直接导入数据源定义。

但是,如果失败,您始终可以手动定义 JDBC 数据源(jdbc url、驱动程序 jar、驱动程序类等)。

配置数据源后,通过在其上打开 SQL 控制台(数据源工具窗口中的“控制台”按钮)进行测试,然后键入一些查询。IDEA 应该在此处提供 SQL 代码补全,用于表名和列名。

如果此步骤有效,请转到数据源的定义,然后调用

“刷新表”。这使得 IntelliJ 检索数据库结构。

接下来,打开“项目结构”(Ctrl-Shift-Alt-S)。选择您的 Hibernate facet(尽管是“Facets”或“Modules”)。

Hibernate facet 的选项屏幕有一个名为“DataSources Mapping”的窗格。在这里,您可以将您的 Hiberante 会话工厂与特定的 IntelliJ 数据源相关联。

在这一步之后,SQL 表/列代码完成和验证也应该在 .hbm 文件中工作。

Applies to IDEA 7 also, read the whole thread if necessary.

也适用于 IDEA 7,如有必要,请阅读整个线程。

回答by DarkSquid

We need substantially more information to help you out.

我们需要更多信息来帮助您。

Is this a command line application? What runtime error is given? What IDE are you using? What is the output from enabling hibernate's debug logging?

这是命令行应用程序吗?给出了什么运行时错误?你用的是什么IDE?启用休眠的调试日志的输出是什么?

回答by Mike Pone

Your IDE is telling you it can't find the table. You can change it to just a warning in IDEA so that your project will atleast compile.

您的 IDE 告诉您它找不到该表。您可以在 IDEA 中将其更改为仅警告,以便您的项目至少可以编译。

回答by Mohsen Abasi

To validate a hbm.xml file mappings, just do the following (Intellij Ultimate 2017.1):

要验证 hbm.xml 文件映射,只需执行以下操作(Intellij Ultimate 2017.1):

View -> Tool Windows -> Database -> click (+) sign -> Data Source -> MySQL

You've added a datasource for your database's server. Now you should create the datasource. The next step is adding this datasource to hibernate mappings. Just follow me:

您已为数据库服务器添加了数据源。现在您应该创建数据源。下一步是将此数据源添加到休眠映射。跟着我:

View -> Tool Windows -> Persistence

The Persistence window will be opened. You must see the project listed.

将打开持久性窗口。您必须看到列出的项目。

Right click on the project -> Assign Data Sources...

In the opened window, there should be 2 columns: "Session Factory" and "Data Source". Just add defined datasource above in the second column.

在打开的窗口中,应该有 2 列:“会话工厂”和“数据源”。只需在第二列中添加上面定义的数据源。