hibernate第一个程序报错

小人国的客人 2017-03-03 12:03:00
报错内容
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at StudentTest.main(StudentTest.java:17)
Caused by: org.dom4j.DocumentException: Error on line 1 of document : 应该有伪属性名。 Nested exception: 应该有伪属性名。
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 3 more

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">
<hibernate-configuration>
    <session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
       <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>  
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <mapping resource="com/hibernate/model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Student.hbm.xml内容

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
    <class name="Student" table="student">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
    <property name="name"  />
        <property name="age" />
    </class>
</hibernate-mapping>

Student.java

package com.hibernate.model;
public class Student {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

StudentTest.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args)throws Exception {
Student s=new Student();
s.setId(1);
s.setAge(1);
s.setName("s1");
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}


...全文
166 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小人国的客人 2017-03-03
  • 打赏
  • 举报
回复
刚开始看就报错 求大神
小人国的客人 2017-03-03
  • 打赏
  • 举报
回复
刚刚搞定,想想很奇葩。。。 1、hibernate.cfg.xml与Student.hbm.xml 里面的内容是复制的文档来的,结果报错 试了跟多次之后我重新一个一个字母敲,xml文档不报错了 发现是空格的问题,删掉空格自己输空格就没错了,(心塞塞) 2、如果改完还出错,把包名com.hibernate.model改成model,配置文件相应改试试(没依据,纯碎都试试看,万一好了呢) 3、之后我又报下面这个错,因为少了j2ee的jar包 Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96) at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856) at StudentTest.main(StudentTest.java:19) 我新建java project没有 建web project直接就有 反正没有就添加 在myEclipse安装目录下 E:\myEclipse\Common\plugins\com.genuitec.eclipse.j2eedt.core_10.5.0.me201207171805\data\libraryset\EE_6 这个是我的安装目录的地址

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧