First off, I am quite new to Hibernate and have not read hibernate much. I am following a tutorial from the internet and am trying to see if the code and the steps given there really work. This is a very basic program.
Ok the problem is that when I run the program, and then check the database, the table is created, but it is not populated. (It remains empty) There seem to be no errors or warnings.
I have MySQL 5.1.47 as the database, Netbeans 6.9 as the IDE and Hibernate version 3.5.2
Here are the source codes:
NewClass.java
Code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class NewClass {
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(3);
contact.setFirstName("yoyo");
contact.setLastName("jojo");
contact.setEmail("yoyo_jojo@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
if(session!=null)
{
session.flush();
session.close();}
}
}
}
Contact.java
Code:
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;
}
}
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/MESSAGES</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>
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="Contact" table="CONTACTS">
<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>
The log:run:
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.5.2-Final
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.c3p0.timeout=1800, hibernate.connection.driver_class=, hibernate.c3p0.max_statements=50, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.c3p0.max_size=20, hibernate.c3p0.min_size=5, hibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://localhost/MESSAGES, com.mysql.jdbc.Driver=, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****}
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : contact.hbm.xml
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: Contact -> CONTACTS
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
22 Jun, 2010 4:22:42 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
22 Jun, 2010 4:22:42 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 10
22 Jun, 2010 4:22:42 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
22 Jun, 2010 4:22:42 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/MESSAGES
22 Jun, 2010 4:22:42 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****}
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.47-community
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
22 Jun, 2010 4:22:42 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
22 Jun, 2010 4:22:42 PM org.hibernate.engine.jdbc.JdbcSupportLoader useContextualLobCreation
INFO: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
22 Jun, 2010 4:22:42 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
22 Jun, 2010 4:22:42 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
22 Jun, 2010 4:22:42 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory createRegionFactory
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
22 Jun, 2010 4:22:42 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Check Nullability in Core (should be disabled when Bean Validation is on): enabled
22 Jun, 2010 4:22:42 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
22 Jun, 2010 4:22:42 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: Running hbm2ddl schema update
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: fetching database metadata
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: updating schema
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: table found: MESSAGES.contacts
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: columns: [id, email, lastname, firstname]
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [primary]
22 Jun, 2010 4:22:42 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
Inserting Record
Done
Hibernate: insert into CONTACTS (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
BUILD SUCCESSFUL (total time: 1 second)
Could this have something to do with the line in the log:
INFO: autocommit mode: false
if yes, how should i make it true ?
otherwise, Could someone please tell me why this is happening ?
Thanks a lot in advance !!!!