15

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

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.

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

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.

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.

UPD: source code

Pupil.java - my persistent class

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

<?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-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

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

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

Compile error: cannot resolve table pupils

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Roman
  • 64,384
  • 92
  • 238
  • 332

6 Answers6

22

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:

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.

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • I've already found the same answer (as you can see the question was asked almost a year ago). But anyway it's correct. – Roman May 11 '10 at 10:47
  • @Roman No problem :) Actually, the "Community" user bumped this question this morning, that's why I posted an answer. Oh, and in such case, if you find a solution, feel free to post your own answer and to accept it. – Pascal Thivent May 11 '10 at 10:52
18

To validate a hbm.xml file mappings, just do the following (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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mohsen Abasi
  • 2,050
  • 28
  • 30
1

File>Settings>Editor>Inspections> unchek Unresolved database references in annotations

user3414260
  • 257
  • 2
  • 6
  • You need to give an indication of what limitations and assumptions apply. See more details on how to answer at this link: https://stackoverflow.com/help/how-to-answer – Usama Abdulrehman Jun 17 '20 at 02:26
  • 1
    just hiding warnings does not fix a problem. refer to this answer for the right solution: https://stackoverflow.com/a/44816353/3214777 – Daniel Pop Jul 13 '20 at 06:11
1

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?

DarkSquid
  • 2,646
  • 1
  • 21
  • 19
  • It's compile error, IDE is IntelliJ IDEA, and it should be a famous problem as I understand from the reference. – Roman Jun 09 '09 at 15:08
  • Please answer the other questions so we can help you. And regarding your error: that's not compiled code: the .cfg.xml is merely a configuration file. Perhaps if you link to the sample you're using? – DarkSquid Jun 09 '09 at 15:12
  • Actually, I don't know how to show you the code. I'm almost sure that code is fine and the problem with some settings (or maybe I lost some nessecary jar-file). I'll try to add something – Roman Jun 09 '09 at 15:14
1

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.

Mike Pone
  • 18,705
  • 13
  • 53
  • 68
0

This is not a hibernate issue I guess.

By any chance, if you use IntelliJ IDEA IDE and if IntelliJ IDEA shows a warning that says Cannot resolve table 'some-table-name'.

  1. Check whether you have created a Data Source correctly. Click here to read how to setup MySQL Data Source

  2. Check whether you have configured the Persistence settings correctly on IntelliJ IDEA.

You can find more details about this warning from here too. Hope you find something useful. Cheers!

Dhanusha_Perera07
  • 3,347
  • 5
  • 14
  • 22