gpt4 book ai didi

java - org.hibernate.boot.registry.classloading.spi.ClassLoadingException:无法加载类[emp.hbm.xml]

转载 作者:行者123 更新时间:2023-12-01 17:07:46 27 4
gpt4 key购买 nike

结构体:
我一直在其所属位置传递hbm和cfg文件。
我的cfg和hbm文件都在src .. enter image description here的根目录下
还包括所有罐子。enter image description here
但仍然遇到这个问题..
请帮助某人。我正尝试从过去2天开始搜索解决方案。

Emp.java


public class Emp

{
private int id;
private String name,cmpname;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCmpname() {
return cmpname;
}
public void setCmpname(String cmpname) {
this.cmpname = cmpname;
}

}



Test.java

package com.oehm.hibernatedemo;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {

public static void main(String[] args) {

Emp e = new Emp();
e.setId(1);
e.setName("harsh");
e.setCmpname("vardhan");

try
{
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session sess=factory.openSession();
sess.beginTransaction();
sess.save(e);
sess.getTransaction().commit();

}
catch (HibernateException exception)
{
exception.printStackTrace();
}

}

}


emp.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>
<class name="com.oehm.hibernatedemo.Emp" table="user01">
<id name="id"></id>
<property name="name"></property>
<property name="cmpname"></property>


</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.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools -->

<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.show_sql">true</property>

<mapping class="emp.hbm.xml"/>


</session-factory>

</hibernate-configuration>

最佳答案

使用了错误的属性class而不是resource来包含映射文件。应该:

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

关于java - org.hibernate.boot.registry.classloading.spi.ClassLoadingException:无法加载类[emp.hbm.xml],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61450209/

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