Hi everybody, I'm new in these Forums and in Hibernate, so I have a simple cuestion I'd like to resolve.
I've developed a simple example in a Java Application which connects a MySQL table to insert two rows via Hibernate.
When I run the application, it seems it works correctly, but when I open the table in MySQL client there are no rows in that table...
I think that Commit forces to write in the DataBase...
Here is the code and the result:
---- HIBERNATE.CFG ------
<?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>
<!-- MySQL connection -->
<property name="connection.url">jdbc:mysql://localhost/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">alberto</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- thread is the short name for
org.hibernate.context.ThreadLocalSessionContext
and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/ibermatica/hibernate/honey.hbm.xml" />
</session-factory>
</hibernate-configuration>
--------- HONEY.HBM.XML --------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.ibermatica.hibernate">
<class name="Honey" table="thoney" >
<id name="id" >
<generator class="increment"/>
</id>
<property name="name" type="string"></property>
<property name="taste" type="string"></property>
</class>
</hibernate-mapping>
-------------------------------------
------ THE SESSION FACTORY -----------
package com.ibermatica.hibernate;
import javax.naming.InitialContext;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class InitSessionFactory {
private InitSessionFactory(){
}
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
public static SessionFactory getInstance() {
if (sessionFactory == null)
initSessionFactory();
return sessionFactory;
}
public Session openSession() {
return sessionFactory.getCurrentSession();
}
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
private static synchronized void initSessionFactory() {
/*
* [laliluna] check again for null because sessionFactory may have been
* initialized between the last check and now
*
*/
//Logger log = Logger.getLogger(InitSessionFactory.class);
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
String sessionFactoryJndiName = cfg.getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg.buildSessionFactory();
log.debug("get a jndi session factory");
sessionFactory = (SessionFactory) (new InitialContext()).lookup(sessionFactoryJndiName);
} else{
log.debug("classic factory");*/
sessionFactory = cfg.buildSessionFactory();
}
} catch (Exception e) {
System.err.println("%%%% Error Creating HibernateSessionFactory %%%%");
e.printStackTrace();
throw new HibernateException(
"Could not initialize the Hibernate configuration");
}
}
}
public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
}
----- THE CLIENT WHICH TESTS ------
package com.ibermatica.hibernate;
import java.util.Iterator;
import java.util.List;
//import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TesteoHoney {
//private static Logger log =Logger.getLogger(TesteoHoney.class);
/**
* @param args
*/
public static void main(String[] args){
Honey forestHoney = new Honey();
forestHoney.setName("forest honey");
forestHoney.setTaste("very sweet");
Honey countryHoney = new Honey();
countryHoney.setName("country honey");
countryHoney.setTaste("tasty");
createHoney(forestHoney);
createHoney(countryHoney);
// our instances have a primary key now:
//log.debug(forestHoney);
//log.debug(countryHoney);
listHoney();
}
private static void listHoney() {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
List honeys = session.createQuery("select h from Honey as h")
.list();
for (Iterator iter = honeys.iterator(); iter.hasNext();) {
Honey element = (Honey) iter.next();
System.out.println(element.getName()+" - "+element.getTaste());
//log.debug(element);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
private static void deleteHoney(Honey honey) {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.delete(honey);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
private static void createHoney(Honey honey){
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
session.setFlushMode(org.hibernate.FlushMode.COMMIT);
try {
tx = session.beginTransaction();
session.save(honey);
session.flush();
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
}
--------------------------------------
As I've told before, the System.output lists the records I have inserted, but in the MySQL client my table is always empty.
I make a commit.
Maybe it's something about LockingMode???
Any ideas, please??
This can't be very difficult... :)
Thanks a lot in advance.
Henry.
|