Hibernate error: cannot resolve table

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2009-06-09T14:58:59Z Indexed on 2010/05/11 4:04 UTC
Read the original article Hit count: 323

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about table