刚从官网下了hibernate4.3.4,结果照着官网doc中的示例class,xml写报了两个错误,实在是头疼
实体类:
package cn.edu.jmu.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getAge() {
return age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate. PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> 自动生成建表语句-->
<mapping resource="cn/edu/jmu/hibernate/model/Student.hbm.xml"/>
<mapping class="cn.edu.jmu.hibernate.model.Student"/>
</session-factory>
</hibernate-configuration>
配置文件2:Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.
<hibernate-mapping package="cn.edu.jmu.hibernate.model">
<class name="Student"><!-- 实体类名与表名一致可不写table属性 -->
<id name="id" /><!--主键 -->
<property name="name" column="name"/><!-- 类中的属性名,对应的数据库中的属性名 -->
<property name="age" /><!-- 不写默认两者一样 -->
</class>
</hibernate-mapping>
测试类:StudentTest
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import java.util.*;
import cn.edu.jmu.hibernate.model.*;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tutorial.util.HibernateUtil;
public class StudentTest {
public static void main(String[] args) {
Student st = new Student();
st.setId(6);
st.setName("哈哈");
st.setAge(20);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();//报错之一:21行的。。。
session.beginTransaction();
session.save(st);
session.getTransaction().commit();
}
}
自定义类(测试类中要用到):HibernateUtil
package org.hibernate.tutorial.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
[ 本帖最后由 a740612348 于 2014-3-24 15:31 编辑 ]