0

as i've wrote in title i have specific problem: upload data is working but reading is giving me that kind of error:

TableMetadata:62 - table found: baza.przychodzace INFO 
TableMetadata:63 - columns: [id, wydzial, imie, adres, tekst, data,
miasto, data2, nazwisko, sygnatura] Exception in thread
"AWT-EventQueue-0" org.hibernate.hql.ast.QuerySyntaxException:
przychodzace is not mapped [from przychodzace]

DB schema:

CREATE TABLE IF NOT EXISTS `przychodzace` (   `ID` int(11) NOT NULL
AUTO_INCREMENT,   `IMIE` varchar(32) CHARACTER SET utf8 COLLATE
utf8_bin NOT NULL,   `NAZWISKO` varchar(32) CHARACTER SET utf8 COLLATE
utf8_bin NOT NULL,   `MIASTO` varchar(32) CHARACTER SET utf8 COLLATE
utf8_bin NOT NULL,   `ADRES` varchar(32) CHARACTER SET utf8 COLLATE
utf8_bin NOT NULL,   `DATA` date NOT NULL,   `DATA2` date NOT NULL,  
`WYDZIAL` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,  
`SYGNATURA` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, 
`TEKST` varchar(3000) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,  
PRIMARY KEY (`ID`) ) ENGINE=InnoDB  DEFAULT CHARSET=utf32
COLLATE=utf32_polish_ci AUTO_INCREMENT=4 ;

hibernate.cf:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/baza</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">validate</property>
        <mapping resource="xml/maps/Maps.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Maps.hbm.xml

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

    <hibernate-mapping package="xml.maps">
        <class name="przychodzace.model.PrzychodzaceModel" table="przychodzace">
            <id name="przychodzaceId" column="ID">
                <generator class="native"/>
            </id>
            <property name="imie" column="IMIE"/>
            <property name="nazwisko" column="NAZWISKO"/>
            <property name="miasto" column="MIASTO"/>
            <property name="adres" column="ADRES"/>
            <property name="data" type="date" column="DATA"/>
            <property name="data2" type="date" column="DATA2"/>
            <property name="wydzial" column="WYDZIAL"/>
            <property name="sygnatura" column="SYGNATURA"/>
            <property name="tekst" column="TEKST"/>
        </class>
    </hibernate-mapping>

PrzychodzaceControler.java where everythings happen

public void zapis(PrzychodzaceModel model) {
    model = save(model);
}

public List<PrzychodzaceModel> read() {
    HibernateUtil hib = new HibernateUtil();
    return list();

}

private static List list() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    List przychodzace = session.createQuery("from przychodzace").list();
    System.out.println(przychodzace.size() + "size");
    session.getTransaction().commit();
    session.close();
    return przychodzace;
}

private static PrzychodzaceModel save(PrzychodzaceModel model) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    session.beginTransaction();

    int id = (Integer) session.save(model);
    model.setPrzychodzaceId(id);

    session.getTransaction().commit();

    session.close();

    return model;
}

Thanks in advance i have no idea what can be wrong

1 Answer 1

2

Change

session.createQuery("from przychodzace").list();

to

session.createQuery("from "+PrzychodzaceModel.class.getSimpleName()).list();

You are using hql you have to use class names not table names.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You very much for so quick answer, of course now it is working. I thought that if it is createQuery i need to put there table name. przychodzace.model.PrzychodzaceModel wasn't needed. Merry Xmas and thanks ;)
The idea of using an ORM it's to abstract about relational model and think in an object way :D
@user3131128 Please read thru the basics of Hibernate and get the basic understanding first. Keep on "wild-guessing" is not an effective way to learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.