异常信息:Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML 把下面的信息换成你的hbm.xml开头部分 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
没问题阿,我用的是MyEclipse10.7+SQL2012,是不是我版本的问题啊
创建Configuration对象的时候、传入你配置文件的路径: Configuration conf = new Configuration().configure("/hibernate.cfg.xml");
这个文件你放在哪个目录下面的?
把下面的信息换成你的hbm.xml开头部分
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Configuration conf = new Configuration().configure("/hibernate.cfg.xml");
默认就是src下面的,
你hibernate的版本是多少?
我记得以前有人发过帖子,hiberntate的版本不同,加载方式不同。
几?
4.1.8 不让用buildSessionFactory() 方法了
这是4.1.8之后的方法:
public static void main(String[] args) {
Student s=new Student();
s.setId(1);
s.setName("penglei");
s.setAge(25);
Configuration config=new Configuration().configure();
ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sf=config.buildSessionFactory(sr);
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}最后发一下感叹,这是我半年前在CSDN上看到一个问题,半年后竟然能找到原帖子,百度真强大。!~
你得换一个3楼给出的dtd
现在变成介个了
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
把中间的空格去掉。