如题:很简单的问题啊,小弟都弄了两天了啊,还没有搞定,
数据库ch3,表:students_table 有四个字段,ID(主键),stuName(varchar),cardId(varchar),age(int),
Student.java
package model;
/**
*
* @author Administrator
*/
public class Student {
private String name;
private String cardId;
private int age;
public Student() {
name = "";
cardId = "";
age = 0;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" table="student_table" name="model.Student" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="id" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="cardId" type="String"/>
<property name="name" type="String"/>
<property name="age" type="int"/>
</class>
</hibernate-mapping>
HibernateUtil.java
package persistence;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.*;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author Administrator
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() {
Session s = sessionFactory.openSession();
return s;
}
public static void closeSession(Session s) {
if (s!= null) {
s.close();
}
}
}
DAOImp.java
package persistence;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.*;
import model.Student;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author Administrator
*/
public class DAOImp {
static Session session=null;
//创建新的学生对象
public static void createStu(Student stu){
try{
session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(stu);
tx.commit();
}catch(HibernateException e){
System.out.print(e.toString());
}finally{
HibernateUtil.closeSession(session);;
}
}
}
BM.java
package BusinessManager;
import persistence.*;
import model.Student;
/**
*
* @author Administrator
*/
public class BM {
public static void main(String args[]){
Student stu=new Student();
stu.setName("qunqun");
stu.setCardId("05051050218");
stu.setAge(22);
DAOImp.createStu(stu);
}
}
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="hibernate.show_sql">true
</property>
<property name="hibernate.connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ch3
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.username">
sa
</property>
<property name="hibernate.connection.password">
</property>
<mapping resource="persistence/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
现运行错误如下:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Copying 1 file to F:\JSP\Hibernate\build\web\WEB-INF\classes
compile-single:
run-main:
2008-9-16 18:03:06 org.hibernate.cfg.Environment <clinit>
信息: Hibernate 3.2.5
2008-9-16 18:03:06 org.hibernate.cfg.Environment <clinit>
信息: hibernate.properties not found
2008-9-16 18:03:06 org.hibernate.cfg.Environment buildBytecodeProvider
信息: Bytecode provider name : cglib
2008-9-16 18:03:06 org.hibernate.cfg.Environment <clinit>
信息: using JDK 1.4 java.sql.Timestamp handling
2008-9-16 18:03:06 org.hibernate.cfg.Configuration configure
信息: configuring from resource: /hibernate.cfg.xml
2008-9-16 18:03:06 org.hibernate.cfg.Configuration getConfigurationInputStream
信息: Configuration resource: /hibernate.cfg.xml
2008-9-16 18:03:06 org.hibernate.cfg.Configuration addResource
信息: Reading mappings from resource : persistence/Student.hbm.xml
2008-9-16 18:03:06 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
信息: Mapping class: model.Student -> student_table
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource persistence/Student.hbm.xml
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class persistence.HibernateUtil
at persistence.DAOImp.createStu(DAOImp.java:31)
at BusinessManager.BM.main(BM.java:19)
Java Result: 1