Hibernate入门

qunqun886 2008-09-16 06:11:35
如题:很简单的问题啊,小弟都弄了两天了啊,还没有搞定,
数据库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

...全文
162 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qunqun886 2008-09-23
  • 打赏
  • 举报
回复
3楼的哥们谢了啊,说了一个错误啊,但是问题还是没有解决了啊,呜呜
hyylcz4 2008-09-16
  • 打赏
  • 举报
回复
<property name="cardId" type="String"/>
<property name="name" type="String"/>
<property name="age" type="int"/>
</class>

你这里写错了,string 是小写,java.lang.String 才是大写
qunqun886 2008-09-16
  • 打赏
  • 举报
回复
包导入了的啊
zhangjin137133 2008-09-16
  • 打赏
  • 举报
回复
可能你的jar包没有导入到web-InF下面

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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