我正在尝试使用休眠模式连接到Oracle,但是却遇到未映射表的异常。我尝试使用XML文件和注释。结果是一样的。这是代码:

数据库中的表名-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();
    }
}


例外:
enter image description here

最佳答案

您必须将hbm移至resources文件夹,以便在类路径中可见。

您的完整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>


调试代码后,我在您的HibernateConfiguration中发现了一个错误,这是正确的代码

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);
        }
    }


您从未在standardServiceRegistryBuilder上调用configure。

我已经更新了此答案和请求请求

pull request解决您的问题

关于java - 表未映射 hibernate ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57644949/

10-10 10:39