【问题标题】:Hibernate never executes queriesHibernate 从不执行查询
【发布时间】:2016-08-29 02:18:39
【问题描述】:

我目前正在学习 Hibernate。我想用一个简单的选择查询来测试 Hibernate,但它从不运行主文件。它可以整天卡在:

INFO: HHH000397: Using ASTQueryTranslatorFactory

这是日志:

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!

类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!");
}
}

类 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;
}
}

类 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;
}
}

【问题讨论】:

  • 如果你没有对结果做任何事情,你怎么知道它没有成功 list() 任何东西?你调试了吗?
  • 它确实执行了查询,因为 Hello World 被打印了两次。
  • private Session session = getSessionFactory().getCurrentSession(); 你不需要这个。

标签: hibernate


【解决方案1】:

首先,要查看日志中的 SQL 查询,请将其放入 hibernate.properties

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

查询未执行,因为它位于窗口底部 仍在运行。

这是因为你没有关闭会话工厂

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 我正在使用 spring-jpa 而不是直接休眠。我没有任何直接代码打开任何休眠会话。任何线索为什么我会遇到此错误?理解这类错误的问题是难以重现它们。
  • @comiventor 可能你需要通过其他方式记录SQL stackoverflow.com/a/37464963/3405171
猜你喜欢
  • 1970-01-01
  • 2013-06-10
  • 2011-03-08
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多