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>