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>

解决方案 »

  1.   

    刚刚接触hibernate!求大神解答!
      

  2.   

    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>赖好给个端口撤
      

  3.   

    这是我改过的,原本那个也不行,原本的就是<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
      

  4.   

    我顶一下!今天重新打了一次代码,还是报错了!
    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();

    }
    }
      

  5.   

    把错误贴出来啊,这样看不出来哦!也是猜测错误,不能准确定位!
    我看了一下映射文件有错误主键要有主键的生成策略吧“<id name="id">
    <generator class="native"></generator>
          </id>”
      

  6.   

    SessionFactory sf = cfg.configure().buildSessionFactory();//这行报错的!
    lz  很明显  是红字的地方错啦!!!
    你进去  在调试下  看红字的方法 走到哪错再贴出来!!