-1

我正在学习休眠,当我运行以下程序时,我收到这条消息: 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;
  }

}

以下是我的休眠配置和 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

4 回答 4

2

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

                Transaction tx = session.beginTransaction();
                session.save(contact);
                 tx.commit();
于 2013-07-26T05:30:22.410 回答
2

transaction在保存中使用。

session.getTransaction().begin();
session.save(contact);
session.getTransaction().commit();
于 2013-07-26T05:30:52.597 回答
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();**
于 2013-07-26T05:32:13.617 回答
1

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

session.getTransaction().commit();

在你之后

session.save(contact);
于 2013-07-26T05:29:49.470 回答