-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Database save Problem
PostPosted: Tue Jun 23, 2009 3:17 am 
Newbie

Joined: Tue Jun 23, 2009 3:06 am
Posts: 2
Hello I am an absolute newbie in Hibernate.

I am having an problem. When I am saving data no error message is coming but the data is not being saved to MySQL database.

Please help.
Code follows:

The POJO:
Code:
package roseindia.tutorial.hibernate;


public class Contact
        {
   private String firstName;
   private String lastName;
   private String email;
   private char 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 char getId()
                    {
      return id;
   }

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

}


Code:
contact.hbm.xml

Code:
<?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="roseindia.tutorial.hibernate.Contact" table="Contact">

     <id name="id" type="char" 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>


Code:
hibernate.cfg.xml


Code:
<?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/doeacc</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</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>



[code]Actual Java Code[/code]
[code]
package roseindia.tutorial.hibernate;

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



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

      try{
         // This step will read hibernate.cfg.xml and prepare hibernate for use
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          session =sessionFactory.openSession();
            //Create new instance of Contact and set values in it by reading them from form object
             System.out.println("Inserting Record");
            Contact contact = new Contact();
            contact.setId('6');
            contact.setFirstName("Deepak");
            contact.setLastName("Kumar");
            contact.setEmail("deep_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();

         }
      
   }
}


Top
 Profile  
 
 Post subject: Re: Database save Problem
PostPosted: Tue Jun 23, 2009 7:44 am 
Beginner
Beginner

Joined: Fri Feb 13, 2009 5:27 am
Posts: 36
Location: India
Hi..dg.souvik....
edit ur code (always use transaction )



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

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();


System.out.println("Inserting Record");
Transaction tx = session.beginTransaction();

Contact contact = new Contact();
contact.setId('6');
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deep_38@yahoo.com");
session.save(contact);
tx.commit();
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();

}

}
}


Bye tc

_________________
parag


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.