import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.eason.hibernate.model.Student;
@SuppressWarnings("unused")
public class StudentTest {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration();
//ServiceRegistryBuilder serviceRegistry = new ServiceRegistryBuilder();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}}
hibernate.cfg.xml:<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">369369</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialectt</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.internal.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="com/eason/hibernate/model/Student.hbm.xml"/> </session-factory></hibernate-configuration>Student.hbm.xml:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.eason.hibernate.model">
<class name="student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.eason.hibernate.model.Student;
@SuppressWarnings("unused")
public class StudentTest {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration();
//ServiceRegistryBuilder serviceRegistry = new ServiceRegistryBuilder();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}}
hibernate.cfg.xml:<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">369369</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialectt</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.internal.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="com/eason/hibernate/model/Student.hbm.xml"/> </session-factory></hibernate-configuration>Student.hbm.xml:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.eason.hibernate.model">
<class name="student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
解决方案 »
- java的继承,那么我们要如何去理解它呢?
- ssh中一个小问题,希望大家help
- Strur2中<S:select>问题急!
- structs2配置文件中返回success成功后,怎么让重定向的文件是可变的???
- sql2000端口号的问题,大家帮帮忙。
- 无法生成cookie问题
- hibernate Criteria查询单个对象怎么查询
- 江湖救急!!怎么调用已由wdsl文件生成的接口类?怎么把接收到的数据取出来!!!!!
- 请问在Red Hat Linux9下,有哪个比较好的Java开发工具!高手帮忙!
- 一个关于jms的低级问题
- 统计下java开发环境 大家可以发自己的机器配置来看看 我先发一个
- 关于java调用批处理命令,怎么才能将消耗降到最低?
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();//这行报错的!
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
我看了一下映射文件有错误主键要有主键的生成策略吧“<id name="id">
<generator class="native"></generator>
</id>”
lz 很明显 是红字的地方错啦!!!
你进去 在调试下 看红字的方法 走到哪错再贴出来!!