-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate don't do the transaction with the mySQL database
PostPosted: Sun May 12, 2013 2:54 pm 
Newbie

Joined: Sun May 12, 2013 2:08 pm
Posts: 5
Hello,

I'm newbe so I thik that I followed correctly the configuration manual.

This is the file hibernate.cfg.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>

      <!-- Database connection settings -->
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bbw</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</property>

      <!-- JDBC connection pool (use the built-in)
      <property name="connection.pool_size">1</property> -->

      <!-- SQL dialect
      <property name="dialect">org.hibernate.dialect.HSQLDialect</property> -->
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

      <!-- Enable Hibernate's automatic session context management -->
      <property name="hibernate.current_session_context_class">thread</property>
      
      <!-- Transaction properties -->
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

      <!-- Echo all executed SQL to stdout -->
      <property name="show_sql">true</property>
      
      <mapping resource="Usuario.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


The file that represents the Java class to map is:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 28, 2012 4:05:29 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
   <class name="registrar.model.Usuario" table="usuario" catalog="bbw">
      
      <id name="usuario" type="string">
         <column name="usuario" />
         <generator class="assigned" />
      </id>
      
      <property name="nombre" type="string">
         <column name="nombre" length="15" />
      </property>
      
      <property name="apellido" type="string">
         <column name="apellido" length="10" />
      </property>
      
      <property name="NIFCIF" type="string">
         <column name="NIFCIF" length="9" />
      </property>
      
      <property name="email" type="string">
         <column name="email" length="30" />
      </property>
      
      <property name="telefono" type="string">
         <column name="telefono" length="9" />
      </property>
      
      <property name="empresa" type="string">
         <column name="empresa" length="15" />
      </property>
      
      <property name="departamento" type="string">
         <column name="departamento" length="10" />
      </property>
      
      <property name="contrasena" type="string">
         <column name="contrasena" length="15" />
      </property>

   </class>
</hibernate-mapping>


I manage the hibernate session with this java classe:

Code:
package utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   static {
      try {
         // Create the SessionFactory from standard (hibernate.cfg.xml)
         // config file.
         Configuration configuration = new Configuration();
         configuration = configuration.configure();
         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
               .applySettings(configuration.getProperties())
               .buildServiceRegistry();
         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      } catch (Throwable ex) {
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
}


The problem is that the database did not insert any, so I can see in the console that says:


Code:
hibernate: insert into bbw.usuario (nombre, apellido, NIFCIF, email, telefono, empresa, departamento, contrasena, usuario) values (?, ?, ?, ?, ?, ?, ?, ?, ?)


Can anyone givme a hand?
Thanks!


Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 3:11 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
Please post your client code which is constructing the object being mapped and inserting it using the Session API.


Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 3:13 am 
Newbie

Joined: Sun May 12, 2013 2:08 pm
Posts: 5
Hello,

I solved the problem.

thanks


Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 3:25 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
Always a good practice for posting how you solved the problem, so that it is easy for someone to know that you do not need assistance in the topic any more and so that other people can learn too.


Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 3:38 am 
Newbie

Joined: Sun May 12, 2013 2:08 pm
Posts: 5
Hello again,

Well I did the query using SQL and I tryied doing with HQL and this worked.

Bye!


Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 3:50 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
Not sure why you would need a query for inserting an object using hibernate. May be I misunderstood what you are asking.

Anyways, the standard way would be:

Code:

    Session s = HibernateUtil.currentSession();
    Transaction txn = s.beingTransaction();

    try {

        Foo foo = // construct object
        s.save(foo);

        // Commit the transaction
        txn.commit();

    } catch (Exception e) {

        txn.rollback();

        // Propagate e upwards
    }



Top
 Profile  
 
 Post subject: Re: Hibernate don't do the transaction with the mySQL database
PostPosted: Fri May 17, 2013 4:20 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
I believe commit is not performed earlier when using SQL.
However he should have used commit in finally block later.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


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