我知道这已经被问过十二遍了。今天,我一定很沮丧,因为我尝试了一百种方法来修改xml文件,但我无法弄清楚。
运行驱动程序时出现错误堆栈:
16 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
31 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
31 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
78 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: test_hibernate/hibernate.cfg.xml
78 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: test_hibernate/hibernate.cfg.xml
125 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : test_hibernate/contact.hbm.xml
Exception in thread "main" Could not parse mapping document from resource test_hibernate/contact.hbm.xml
java.lang.NullPointerException
at test_hibernate.MyExample.main(MyExample.java:29)
这是我如何建立表格的MySQL代码:
create database HibernateTest;
drop table contact;
Create table CONTACT(
ID int(15),
FIRSTNAME varchar(50) ,
LASTNAME varchar(50),
EMAIL varchar(150));
describe contact;
这是我的映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test_hibernate.Contact" table="CONTACT">
<id name="id" column="ID" >
<generator class="increment" />
</id>
<property name="firstName" column="FIRSTNAME" />
<property name="lastName" column="LASTNAME" />
<property name="email" column="EMAIL" />
</class>
</hibernate-mapping>
这是我的配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/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/hibernatetest</property>
<property name="hibernate.connection.username">###</property>
<property name="hibernate.connection.password">###</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="test_hibernate/contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
任何帮助表示赞赏,谢谢!
一些其他信息:安装程序包test_hibernate和这是我的持久性文件:
package test_hibernate;
public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmail(String string) {
email = string;
}
public void setFirstName(String string) {
firstName = string;
}
public void setLastName(String string) {
lastName = string;
}
public long getId() {
return id;
}
public void setId(long l) {
id = l;
}
}
这是我的司机:
package test_hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class MyExample {
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
session=sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Smitha");
contact.setLastName("Rao");
contact.setEmail("smithaxxx@yahoo.com");
session.save(contact);
tx.commit();
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
您的映射文件看起来不错。我认为这可能是由于您在双引号中使用了 “ 或 ” 而不是 “ 。”请尝试在所有双引号中使用 “ 。
当我尝试用Maven编译时,我得到了这个错误: 但我看不出错误在哪里: user.java
我正在遵循本教程,并得到以上错误。我试过这个和这个,但都没有解决问题(我的模型类所在的com.spring.hibernate.model下有user.hbm.xml,而class-path(src/main/java)下有hibernate.cfg.xml文件)。有谁能帮忙吗? user.hbm.xml hibernate.cfg.xml 我正在使用mysql,这是我更新的cfg文件
Hi在运行我的java类时得到以下错误:创建会话工厂失败,出现异常org.hibernate.mappingException:无法从资源中读取映射:software.hbm.xml org.hibernate.cfg.configuration.addResource(configuration.java:485),在org.hibernate.cfg.cfiguration.parsemapp
问题内容: 我的maven spring项目目录结构如下所示。我正在使用基于Spring-4注释的配置。我按如下方式配置资源。我尝试了许多Stackoverflow问题和其他网站中建议的许多方法 但是jsp文件无法加载资源,所有静态内容请求均返回404错误。我在jsp中尝试了这些东西, 编辑:我正在使用Servlet 2.5,因为到目前为止,我无法将项目从JBoss 5升级到更高版本。JBoss5