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 class="native" />
</id>
<property name="name" column="name" type="string" />
</class>
</hibernate-mapping>
一运行就报错,错误如下:
四月 13, 2014 10:48:36 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
四月 13, 2014 10:48:36 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
四月 13, 2014 10:48:36 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
四月 13, 2014 10:48:36 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
四月 13, 2014 10:48:36 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
四月 13, 2014 10:48:36 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
四月 13, 2014 10:48:36 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
四月 13, 2014 10:48:36 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: cn/itcast/helloWorld/User.hbm.xml