poldeeek
poldeeek

Reputation: 333

Table is not mapped Hibernate

I'm trying to connect to Oracle with hibernate however I'm getting exception that table is not mapped. I tried to use XML files and annotations. The result was the same. Here is code:

Table name in database - NOWA

NOWA.java

    package entity;

public class NOWA {
    private int id;
    private String imie;

    @Override
    public String toString() {
        return "Nowa{" +
                "id=" + id +
                ", imie='" + imie + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getImie() {
        return imie;
    }

    public void setImie(String imie) {
        this.imie = imie;
    }
}

NOWA.hbm.xml

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

<hibernate-mapping package="entity">
    <class name = "NOWA" table = "NOWA">
        <id name="id" type="int" column="ID" >
            <generator class="assigned"></generator>
        </id>

        <property name="imie" column="IMIE" />

    </class>

</hibernate-mapping>

hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">***</property>
        <property name="hibernate.connection.username">***</property>
        <property name="hibernate.connection.password">***</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping resource="entity/NOWA.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Controller.java

    package sample;

import entity.NOWA;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import org.hibernate.Session;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @FXML
    private ListView<NOWA> tabela;


    public void initialize(URL location, ResourceBundle resources) {

        Session session = HibernateFactory.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<NOWA> lista = session.createQuery("from NOWA ").list();
        tabela.getItems().setAll(lista);
        for (NOWA a: lista
             ) {
            System.out.println(a);
        }

        session.getTransaction().commit();
        session.close();
    }
}

Exception: enter image description here

Upvotes: 0

Views: 242

Answers (2)

vincenzopalazzo
vincenzopalazzo

Reputation: 1645

You must move your hbm to the resources folder so that it is visible in the classpath.

Your complete XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">***</property>
        <property name="hibernate.connection.username">***</property>
        <property name="hibernate.connection.password">***</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping resource="entity/NOWA.hbm.xml" />

        <mapping class="entity.NOWA" />

    </session-factory>
</hibernate-configuration>

After Debugging your code I have finding a bug in the your HibernateConfiguration, this is the code correct

private static SessionFactory buildSessionFactory() {
        try {


            Configuration configuration = new Configuration();
            configuration.configure();
            StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
            standardServiceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = standardServiceRegistryBuilder.configure().build();

            return configuration.buildSessionFactory(serviceRegistry);
        }
        catch(Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

you never called configure on standardServiceRegistryBuilder.

I have updated this answer and the pull-request

This pull request fix your problem

Upvotes: 1

utkarsh sharma
utkarsh sharma

Reputation: 21

oracle.jdbc.driver.OracleDriver *** thread

    <mapping resource="NOWA.hbm.xml" />



</session-factory>

Upvotes: 0

Related Questions