-->
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: How to perform a simple Commit?
PostPosted: Wed Aug 30, 2006 3:20 am 
Newbie

Joined: Wed Aug 30, 2006 3:06 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 3:40 am 
Newbie

Joined: Wed Aug 30, 2006 3:06 am
Posts: 2
Hi!

Nobody knows anything about this?? :)

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 7:36 am 
Beginner
Beginner

Joined: Fri Sep 08, 2006 7:29 am
Posts: 36
Nothing seems wrong with ur code.

Just to give a try....set auto_commit='true' in hibenate.cfg.xml.

Br
Shardul.


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.