当前位置: 首页 > 知识库问答 >
问题:

Hibernate从不执行查询

柴耀
2023-03-14
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!
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!");
}
}
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;
}
}
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;
}
}

共有1个答案

庄星汉
2023-03-14

首先,要查看日志中的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();
        }
    }

}
 类似资料:
  • 执行获取HQL查询时出现异常。查询大部分时间都有效,但有时会显示此异常 数据库是MySQL,使用的服务器是JBoss 5.1.0 GA 显示的错误是: 组织。冬眠例外GenericJDBCException:无法在组织上执行查询。冬眠例外SQLStateConverter。在组织中处理非特定异常(SQLStateConverter.java:126)。冬眠例外SQLStateConverter。o

  • 我的桌子描述是 我的疑问是 如何使用hibernate执行此查询。Hibernate应该返回类型为的对象列表。?

  • 问题陈述:在JPA hibernate中,我执行了一个方法

  • 一旦你建立好数据模型之后,django会自动生成一套数据库抽象的API,可以让你执行增删改查的操作。这篇文档阐述了如何使用这些API。关于所有模型检索选项的详细内容,请见数据模型参考。 在整个文档(以及参考)中,我们会大量使用下面的模型,它构成了一个博客应用。 from django.db import models class Blog(models.Model): name = mo

  • 问题内容: Hibernate不支持union,所以我想单独运行sql。但是最后如何合并这些值? 请告知如何分别执行两个sql,最后如何合并这些值? 问题答案: 请注意,UNION中的每个SELECT语句必须具有相同的列数。 这些列还必须具有相似的数据类型。另外,每个SELECT语句中的列必须具有相同的顺序。 如果是这样,请向您的查询添加别名: 您可以通过以下方式使用SQLQuery和AliasT

  • 问题内容: 我正在使用Hibernate 3.1.1,尤其是我正在使用HQL查询。 根据文档,Hibernate的查询是多态的: 像:这样的查询不仅返回的实例,还返回像的子类的实例。 如何查询Cat的实例,但不查询其任何子类的实例? 我希望能够做到而不必明确提及每个子类。 我知道以下选项,但并不令人满意: 查询后手动过滤实例,或者: 在鉴别符列上手动添加WHERE子句。 Hibernate允许用户