休眠数据未插入数据库

问题描述

你好,我正在使用简单的休眠示例,我对休眠不了解更多 在这里,我的示例运行成功,但是我不明白数据库中没有插入数据 这是我的代码
package com;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * @author Deepak Kumar
 *
 * http://www.roseindia.net
 * Hibernate example to inset data into Contact table
 */
public class FirstExample {
  public static void main(String[] args) {
  Session session = null;

  try{
  // This step will read hibernate.cfg.xml 


  SessionFactory sessionFactory = new 

Configuration().configure().buildSessionFactory();
 session =sessionFactory.openSession();
  //Create new instance of Contact and set 


 System.out.println(\"Inserting Record\");
  ContactDetails contact = new ContactDetails();
  contact.setId(3);
  contact.setFirstName(\"Deepak\");
  contact.setLastName(\"Kumar\");
  contact.setEmail(\"deepak_38@yahoo.com\");
  session.save(contact);
  System.out.println(\"Done\");
  }catch(Exception e){
  System.out.println(e.getMessage());
  }finally{
  // Actual contact insertion will happen at this step
  session.flush();
  session.close();

  }

  }
}








<?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\">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
    <property name=\"hbm2ddl.auto\">update</property>
    <property name=\"dialect\">org.hibernate.dialect.MysqLDialect</property>
    <property name=\"connection.url\">jdbc:MysqL://localhost:3306/hibernateexample</property>
    <property name=\"connection.username\">root</property>
    <property name=\"connection.password\">admin</property>
    <property name=\"connection.driver_class\">com.MysqL.jdbc.Driver</property>
    <property name=\"myeclipse.connection.profile\">hibernateexample</property>
    <mapping resource=\"contact.xml\" />

</session-factory>

</hibernate-configuration>





package com;

public class ContactDetails {
  private String firstName;
  private String lastName;
  private String email;
  private long id;

  /**
 * @return Email
 */
  public String getEmail() {
  return email;
  }

  /**
 * @return First Name
 */
  public String getFirstName() {
  return firstName;
  }

  /** 
 * @return Last name
 */
  public String getLastName() {
  return lastName;
  }

  /**
 * @param string Sets the Email
 */
  public void setEmail(String string) {
  email = string;
  }

  /**
 * @param string Sets the First Name
 */
  public void setFirstName(String string) {
  firstName = string;
  }

  /**
 * @param string sets the Last Name
 */
  public void setLastName(String string) {
  lastName = string;
  }

  /**
 * @return ID Returns ID
 */
  public long getId() {
  return id;
  }

  /**
 * @param l Sets the ID
 */
  public void setId(long l) {
  id = l;
  }

}


<?xml version=\"1.0\"?>
<!DOCTYPE hibernate-mapping PUBLIC 
\"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"
\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">

<hibernate-mapping>
  <class name=\"com.ContactDetails\" table=\"CONTACT\">
   <id name=\"id\" type=\"long\" column=\"ID\" >
   <generator class=\"assigned\"/>
  </id>

  <property name=\"firstName\">
   <column name=\"FirsTNAME\" />
  </property>
  <property name=\"lastName\">
  <column name=\"LASTNAME\"/>
  </property>
  <property name=\"email\">
  <column name=\"EMAIL\"/>
  </property>
 </class>
</hibernate-mapping>
为什么数据没有插入数据库     

解决方法

        尝试使用事务。 将代码添加到FirstExample类中。
Transaction tx=session.beginTransaction(); // Add this soon after initializing session object

tx.commit() // just before saving the session
    ,        映射应为 并且XML文件的名称应相同。 查看本教程: http://docs.jboss.org/hibernate/core/3.3/reference/zh/html/tutorial.html