日期:2014-05-18  浏览次数:20728 次

急!初学Hibernate,弄了2天,找不到错误...
package cn.itcast.helloWorld;

public class User {
private int id;
private String name;

public User(){}

public User(int id,String name){
this.id=id;
this.name=name;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


测试类:
package cn.itcast.helloWorld;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App {
public static void main(String args[]) {
User user=new User(1,"张三");
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction ts=session.beginTransaction();
session.save(user);
ts.commit();
session.close();
}


}


hebirnate.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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=Test</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">5782706007</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="show_sql">true</property>

        <mapping resource="cn/itcast/helloWorld/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

User.hbm.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
        '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
        'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>

<hibernate-mapping package="cn.itcast.helloWorld" >

    <class name="User" table="user">

        <id name="id" type="int" column="id">
            <generator clas