[英]I am getting error in: Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
I am getting this Exception 我收到此异常
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:13)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
My hibernate.cfg file is 我的hibernate.cfg文件是
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3006/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/jwt/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
My Mapping File is: 我的映射文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.jwt.hibernate.Student" table="STUDENT">
<id column="ID" name="id" type="int" />
<property column="STUDENT_NAME" name="name" type="string" />
<property column="DEGREE" name="degree" type="string" />
<property column="ROLL" name="roll" type="string" />
<property column="PHONE" name="phone" type="string" />
</class>
</hibernate-mapping>
My Bean class is: 我的Bean类是:
public class Student {
private int id;
private String name;
private String degree;
private String roll;
private String phone;
/** Getters and setters omitted **/
}
My Tester class is: 我的测试人员课程是:
public class SimpleTest {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Student student = new Student();
student.setName("Gourab");
student.setRoll("101");
student.setPhone("8888");
student.setDegree("B.E");
Transaction tx = session.beginTransaction();
session.save(student);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
My Folder Structure is 我的文件夹结构是
I have added all required jars for connecting to hibernate and mysql(eg hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar) But still I am getting the error. 我已经添加了连接到hibernate和mysql所需的所有jar(例如,hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar),但仍然出现错误。 Please Help me out.
请帮帮我。 Thanks in advance
提前致谢
Full Stack Trace: 全栈跟踪:
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:12)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 more
I copied every file from your page as above and created a new project. 我如上所述从您的页面复制了每个文件,并创建了一个新项目。 Please follow the below structure to avoid the above exception.
请遵循以下结构以避免上述异常。
public class App
{
public static void main( String[] args )
{
System.out.println("Maven + Hibernate + MySQL");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Student st1 = new Student();
st1.setName("srinivas");
st1.setPhone("99");
st1.setRoll("123");
session.save(st1);
session.getTransaction().commit();
}
}
Hibernate can't parse your hibernate.cfg.xml
. Hibernate无法解析您的
hibernate.cfg.xml
。
hibernate-core-3.6.9.Final.jar
has a file with DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd
hibernate-core-3.6.9.Final.jar
的文件带有DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd
Check this file inside hibernate-core-3.6.9.Final.jar
. 在
hibernate-core-3.6.9.Final.jar
检查此文件。
You need to have the same DTD
as hibernate-configuration-3.0.dtd
has. 您需要具有与
hibernate-configuration-3.0.dtd
相同的DTD
。
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Looks like you already have it. 看起来您已经拥有了。
May be you have other hibernate-core
jar in the class path with other DTD. 可能是您在类路径中与其他DTD一起使用了其他
hibernate-core
jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.