我在努力学习如何使用Hibernate。我在教程之后创建了一个小项目,但是当我尝试运行它时,它会抛出以下异常:http://pastebin.com/pu2FnmmT
在我看来hibernate.cfg.xml是找不到的。我知道以前可能有人问过这个问题,所有的解决方案都说我必须将hibernate.cfg.xml放在/src文件夹中。这就是我所做的,但由于某种原因,这个错误不断出现。
在下面我粘贴了所涉及的代码:
HibernateUtil.java
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import model.Device;
public class HibernateUtil {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistery = builder.build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistery);
Session session = sessionFactory.openSession();
Device device = new Device();
device.setId(4);
device.setName("Test Smart TV");
Transaction tx = session.beginTransaction();
session.save(device);
tx.commit();
System.out.println("Object saved successfully.....!!");
session.close();
sessionFactory.close();
}
}hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/poc</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to sysout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Mapping file -->
<mapping resource="device.hbm.xml" />
</session-factory>
</hibernate-configuration>我的项目结构如下:https://puu.sh/tl50v/31cb4b0da9.png
如有任何帮助或建议,将不胜感激。
发布于 2017-01-16 10:23:26
很抱歉我周末没能回复。我已经修复了它,似乎我下载了一个不正确版本的hibernate,它没有包含所有需要的.JAR文件。
在替换了这些之后,它就像一种魅力。谢谢你们的帮助!案子结案了!
发布于 2017-01-13 17:44:39
如果您使用eclipse "Run >“选项运行代码.将该文件(hibernate.cfg.xml)作为src文件夹的silibing文件.
发布于 2017-01-13 19:03:21
你有没有说:
<hibernate-mapping package="model">在device.hbm.xml?
并检查您的jar在您的项目中是否正确。
当我使用hibernate时,我不会在配置中给出"hibernate.cfg.xml“,而是将
没有冬眠。
https://stackoverflow.com/questions/41640068
复制相似问题