gpt4 book ai didi

java - 表未映射 Hibernate

转载 作者:行者123 更新时间:2023-12-02 09:34:36 25 4
gpt4 key购买 nike

我正在尝试使用 hibernate 连接到 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 移至资源文件夹,以便它在类路径中可见。

您的完整 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 上调用过配置。

我已经更新了这个答案和拉取请求

这个pull request解决你的问题

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com