-->
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.  [ 3 posts ] 
Author Message
 Post subject: No errors but no save
PostPosted: Tue Oct 27, 2009 5:18 pm 
Newbie

Joined: Tue Oct 27, 2009 4:57 pm
Posts: 1
New Hibernate user trying to do a simple update per an example. The program runs without any errors, but the record I'm saving doesn't appear in the database.

Using Java 1.6 and mySQL.

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

    try
      {
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      session =sessionFactory.openSession();
      System.out.println("Inserting Record");
      Contact contact = new Contact();
      contact.setId(6);
      contact.setFirstName("Deepak");
      contact.setLastName("Kumar");
      contact.setEmail("deepak_38@yahoo.com");
      System.out.println("ID: " + contact.getId()
        + "\nFirst name: " + contact.getFirstName()
        + "\nLast name: " + contact.getLastName()
        + "\nEmail: " + contact.getEmail());
      session.save(contact);
      System.out.println("Done");
      }
    catch(Exception e)
      {
      System.out.println(e.getMessage());
      System.exit(1);
      }
    session.flush();
    session.close();
    }
  }

public class Contact
  {
  private String firstName;
  private String lastName;
  private String email;
  private long id;

  public String getEmail()
    { return email; }

  public String getFirstName()
    { return firstName; }

  public String getLastName()
    { return lastName; }

  public void setEmail(String string)
    { email = string; }

  public void setFirstName(String string)
    { firstName = string; }

  public void setLastName(String string)
    { lastName = string; }

  public long getId()
    { return id; }

  public void setId(long l)
    { id = l; }

  }

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


Top
 Profile  
 
 Post subject: Re: No errors but no save
PostPosted: Wed Oct 28, 2009 12:47 am 
Newbie

Joined: Fri Apr 03, 2009 4:12 pm
Posts: 9
hello,

You should try to do your stuff inside a transaction like this:
Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}



_________________
Santiago GarcĂ­a Pimentel R.G.


Top
 Profile  
 
 Post subject: Re: No errors but no save
PostPosted: Wed Oct 28, 2009 6:19 am 
Newbie

Joined: Wed Oct 28, 2009 6:12 am
Posts: 1
u can enable autocommit property in hibernate configuration file. In that case no need to use the Transaction.
but u should know which one is good

<property name="connection.autocommit">true</property>


Read this link

https://www.hibernate.org/403.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.