开发者

Hibernate Data Not inserted in database

hello i am using hibernate simple example and i am not know more about hibernate

here my example is running successful but data are not inserted in the database i can't understand

this is my code

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开发者_StackOverflow中文版 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>

why data not inserted in database


try using transaction. Add the codes in the FirstExample class.

Transaction tx=session.beginTransaction(); // Add this soon after initializing session object

tx.commit() // just before saving the session


The mapping should be and the XML file accordingly should be of the same name. Check out this tutorial : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜