2 Replies - 596 Views - Last Post: 06 January 2016 - 06:59 AM

#1 Isidro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-December 15

Problem with Netbeans interface and Hibernate

Posted 04 January 2016 - 12:25 PM

Hello everyone. I have a problem at entering data into my Database using Hibernate from a Netbeans Interface. The thing is that I'm testing to insert a client into my database in a test class and it works, but If I copypaste the same code to an onclick method of a Netbeans Interface, it shows me an excepcion. Why is this happening? I'm using the same methods and classes for both and I'm entering the same data.

Here's my program:

SessionFactoryUtil class, it gets the configuration of the DB:
import org.hibernate.*;
import org.hibernate.cfg.Configuration;

/*@author Isidro*/

public class SessionFactoryUtil {
    // To get Singleton session instance
    private static final SessionFactory sessionFactory;
    
    static {
        // Getting the properties file required by Hibernate
        Configuration cfg = new Configuration().configure();
        sessionFactory = cfg.buildSessionFactory();
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


ClientManagementDAO class, it inserts data into de DB:
import Control.SessionFactoryUtil;
import Model.Clients;
import java.sql.SQLException;
import java.util.logging.Level;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/*@author Isidro*/

public class ClientManagementDAO {

    static SessionFactory sessionFactory;
    
    public static void turnOnConf () {
        //Crear i obrir la sesió
        sessionFactory = SessionFactoryUtil.getSessionFactory ();
    }
    
    public static void turnOffConf () {
        sessionFactory.close();
    }
    
    public static void insertClient(Clients client) throws SQLException {
        java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);
        turnOnConf();
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        System.out.println(client);
        try {
            tx = session.beginTransaction();
            session.save(client);
            tx.commit();
        } 
        catch (HibernateException e){ 
            if (tx != null ) {
                tx.rollback();
            }
            if (session != null) {
                session.close();
                turnOffConf();
            }
            throw new SQLException();
        } 
        finally { 
            if (session != null) {
                session.close();
                turnOffConf();
            }
        }
    }
}


ClientManagementTest class, with this class, the 2 class above work perfectly
import Control.ClientManagementDAO;
import Model.Clients;
import java.sql.SQLException;

/*@author Isidro*/

public class ClientManagementTest {
    public static void main(String[] args) {
        Clients client = new Clients("12", "Pepito", "Camela", "El que rie", "[email protected]", 
            "Calle del Gorrino Num. 455", null);
        try {
            GestioClientsBD.inserirClient(client);
        }
        catch (SQLException e) {}
        System.out.println("The client has been added sucesfully.");
    }
}


But If I try to insert data into the DB from an onclick button of my Interface, it shows me an Excepcion:

ClientManagementBLL class, it pass information between the interface and ClientManagementDAO class:
import Control.ClientManagementDAO;
import Model.Clients;
import java.sql.SQLException;

/*@author Isidro*/

public class ClientManagementBLL {

    public static void insert (Clients client) throws SQLException {
        try {
            System.out.println(client);
            ClientManagementDAO.insertClient(client);
        }
        catch (SQLException e) {
            throw e;
        }
    }
    
}


And for last here's my code in the onclick method of the interface:
private void bInserirActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            Clients client = new Clients("123", "Pepito", "Camela", "El que rie", "[email protected]", 
                "Calle del Gorrino Num. 455", null);
                ClientManagementBLL.insert(client);
        }
        catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "There has been an error in the database", "Error", 
                JOptionPane.ERROR_MESSAGE);
            dispose();
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "This ID is alredy in the database", "Error", 
                JOptionPane.ERROR_MESSAGE);
            dispose();
        }
    }


The most weird thing is that it shows me an Exception when I have never throw an Exception in the classes of above. I think this error is related with Hibernate and the interface because this classes work without the interface.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Netbeans interface and Hibernate

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,342
  • Joined: 12-December 12

Re: Problem with Netbeans interface and Hibernate

Posted 04 January 2016 - 12:43 PM

You've mentioned an Exception several times but haven't yet told us what it is. Post the full error details.
Was This Post Helpful? 0
  • +
  • -

#3 Isidro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-December 15

Re: Problem with Netbeans interface and Hibernate

Posted 06 January 2016 - 06:59 AM

Yes, sorry. I throw a SQL Exception when there's an Hibernate Exception because for some reason, when I throw an Hibernate Exception, it doesn't throw it correctly. I don't know if there's a problem with Hibernate library or Hibernate Exception have to be throwed by other way. In this program that I posted here there's no classes that throw an Excepcion, all of them throw a SQL Exception.

I catch an Exception because before inserting data, I use another method to confirm if in the DB there isn't a client with the same ID and it throws an Exception, but I tested it alone and it works perfectly, the problem is in the code that I posted.

I tested inserting a client by the same way that I do in the test class and I get the same error, I have tested it and find that the error is in the SessionFactoryUtil class, but I don't know why the error is here because this class works with the test class and moreover, I don't know why I'm getting here an error from a code that I'm not using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1