Hibernate查询执行但没有添加任何内容

时间:2013-07-26 05:25:42

标签: java hibernate

我正在学习休眠,当我运行以下程序时,我收到此消息: Hibernate: insert into contact (firstname, lastname, email, id) values (?, ?, ?, ?)但是当我查看表格时,似乎没有插入任何内容。这有什么问题?很明显,语句值字段为空,但为什么呢?

我有以下POJO:

public class Contact {
  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;
  }

}

以下是我的hibernate配置和hbm文件:

<?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/aneesh</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">123</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="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>




<?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="Contact" table="contact">
   <id name="id"  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>

这是我要插入的代码:

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


public class HSession {
  public static void main(String[] args) {
      Session session = null;

  try{
      SessionFactory sessionGet = new Configuration().configure().buildSessionFactory();

      session = sessionGet.openSession();

     Contact contact = new Contact();

     contact.setFirstName("xyz");
     contact.setLastName("zyx");
     contact.setEmail("x@xmail.com");
     contact.setId(20);
     session.save(contact);



  }finally{
  // Actual contact insertion will happen at this step
  try {
    session.flush();
      session.close();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

  }

  }
}

4 个答案:

答案 0 :(得分:2)

在保存对象之前,您必须获得事务,在保存对象后,您必须提交事务。

                Transaction tx = session.beginTransaction();
                session.save(contact);
                 tx.commit();

答案 1 :(得分:2)

在保存中使用transaction

session.getTransaction().begin();
session.save(contact);
session.getTransaction().commit();

答案 2 :(得分:2)

您忘记了开始并提交了交易。

        session = sessionGet.openSession();
        **Transaction tx= session.beginTransaction();**
        Contact contact = new Contact();

         contact.setFirstName("xyz");
         contact.setLastName("zyx");
         contact.setEmail("x@xmail.com");
         contact.setId(20);
         session.save(contact);
         **tx.commit();**

答案 3 :(得分:1)

您应该提交插件以反映在数据库中 尝试

session.getTransaction().commit();
之后

session.save(contact);