数据未使用hibernate插入,但没有出错?

Kri*_*ris 4 java hibernate

我正在尝试开始使用Hibernate,但由于某些原因无法插入数据.它似乎工作正常,因为没有给出错误,但是当我检查数据库时表是空的.我不认为它与数据库本身的连接失败,因为当我在映射xml中将表名更改为非超级表时,Hibernate会动态创建它(但正如我所说,没有插入数据) .有谁知道是什么问题?

这是我的代码:

hibernate.cfg.xml中:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
          <property     name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intex</property>
          <property name="hibernate.connection.username">root</property>
          <property name="hibernate.connection.password"></property>
          <property name="hibernate.connection.pool_size">10</property>
          <property name="show_sql">true</property>
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="hibernate.hbm2ddl.auto">update</property>
          <!-- Mapping files -->
          <mapping resource="intex.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我的映射xml:

<hibernate-mapping>
  <class name="com.intex.uni.courses.Course" table="course">
   <id name="id" column="id" >
   <generator class="increment"/>
  </id>

  <property name="name" column="name" />
  <property name="description" column="description" />
  <property name="url" column="url" />
  <property name="code" column="code" />
 </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

我的测试客户:

public class HibernateTest {

    public static void main(String[] args) {

        Session session = null;

        try {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            Course course = new Course();
            course.setDescription("Description");
            course.setName("NAME");
            course.setUrl("http://www.url.com");
            session.save(course);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            session.flush();
            session.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我真的希望有人能够帮助我!提前致谢 :)

use*_*019 5

我怀疑系统没有提交包含所需插入的事务.我总是明确地设置事务,例如Hibernate入门教程示例2.5

另一种方法是您应该能够在代码中设置Hibernate的提交模式,以便事务是隐式的 - 看看FlushMode


小智 5

在主类中使用此代码并进行测试.

Session session = null;
Transaction txn = null;
try {  
    SessionFactory sessionFactory = 
        new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();  
    txn = session.beginTransaction();
    Course course = new Course();  
    course.setDescription("Description");
    course.setName("NAME");
    course.setUrl("http://www.url.com"); 
    session.save(course); 
    txn.commit();

} catch (Exception e) { 
    System.out.println(e.getMessage());
} finally {
    if (!txn.wasCommitted()) {
        txn.rollback();
    }

    session.flush();  
    session.close();   
}
Run Code Online (Sandbox Code Playgroud)