2

I am currently studying Hibernate. I wanted to test Hibernate with a simple select query but it never runs the main file. It can be stuck all day on :

INFO: HHH000397: Using ASTQueryTranslatorFactory

this is the log :

run:
mai 03, 2016 8:58:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
mai 03, 2016 8:58:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: dao/javaBeans/Type.hbm.xml
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: dao/javaBeans/Projet.hbm.xml
mai 03, 2016 8:58:30 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
mai 03, 2016 8:58:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
mai 03, 2016 8:58:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:6666/projet]
mai 03, 2016 8:58:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
mai 03, 2016 8:58:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
mai 03, 2016 8:58:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Tue May 03 20:58:30 WEST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
 mai 03, 2016 8:58:31 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
mai 03, 2016 8:58:31 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
mai 03, 2016 8:58:31 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hello wordl!
Hello wordl!

class Test.java

package dao.javaUtil;
import org.hibernate.Session;
public class Test {
static Session session = HibernateUtil.openSession();

public static void main(String[] args)   {
    System.out.println("Hello wordl!");
    session.createQuery("select o from Projet o").list();
    session.close();
    System.out.println("Hello wordl!");
}
}

class HibernateUtil.java :

package dao.javaUtil;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class HibernateUtil {

private static SessionFactory sessionFactory;
private Session session = getSessionFactory().getCurrentSession();

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static Session openSession(){
    return sessionFactory.openSession();
}

public static Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}


public static void close() {
    if(sessionFactory != null) sessionFactory.close();

    sessionFactory = null;
}
}

class Projet.java :

package dao.javaBeans;
 public class Projet  implements java.io.Serializable {


 private Integer idprojet;
 private Type type;
 private String title;
 private String description;
 private Double budget;
 private Character active;

public Projet() {
}


public Projet(Type type) {
    this.type = type;
}
public Projet(Type type, String title, String description, Double budget, Character active) {
   this.type = type;
   this.title = title;
   this.description = description;
   this.budget = budget;
   this.active = active;
}

public Integer getIdprojet() {
    return this.idprojet;
}

public void setIdprojet(Integer idprojet) {
    this.idprojet = idprojet;
}
public Type getType() {
    return this.type;
}

public void setType(Type type) {
    this.type = type;
}
public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}
public Double getBudget() {
    return this.budget;
}

public void setBudget(Double budget) {
    this.budget = budget;
}
public Character getActive() {
    return this.active;
}

public void setActive(Character active) {
    this.active = active;
}
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Chaimae Zkara
  • 21
  • 1
  • 1
  • 2

1 Answers1

1

Firstly, to see SQL queries in the log, put this in the hibernate.properties

hibernate.show_sql=true
hibernate.use_sql_comments=true
hibernate.format_sql=true

The query wasn't executed because at the bottom of the window it is still running.

It is because of you don't close a session factory

public class Test {

    public static void main(String[] args)   {
        try {
          Session session = HibernateUtil.openSession();
          List<Projet> projets = session.createQuery("from Projet").list();
          System.out.println(projets);
          session.close();
        } finally {
           HibernateUtil.close();
        }
    }

}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • @v.ladynev I am using spring-jpa and not hibernate directly. I don't have any direct code opening any hibernate session. Any clues why I am experiencing this error? Problem with understanding these kind of errors is the difficulty to repro them. – comiventor Feb 15 '18 at 17:56
  • @comiventor Probably you need to log SQL by other way https://stackoverflow.com/a/37464963/3405171 – v.ladynev Feb 16 '18 at 22:15