简体   繁体   中英

Hibernate query executes but nothing is added

I'm learning hibernate and when I run the following program, I get this message : Hibernate: insert into contact (firstname, lastname, email, id) values (?, ?, ?, ?) but when I check the table, nothing seems to be inserted. What's the problem here ? Clearly the statement value field is null but why?

I have the following 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;
  }

}

and below are my hibernate config and hbm files:

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

and here is my code to insert :

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();
}

  }

  }
}

Before you are saving the object you have to get the transaction and after saving the object you have to commit the transaction.

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

Use transaction in save.

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

You forgot the begin and commit the transaction.

        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();**

you should do a commit for the insert to be reflected in database try

session.getTransaction().commit();

after your

session.save(contact);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM