Hibernate从不执行查询。
Hibernate从不执行查询。
我目前正在学习Hibernate。我想用一个简单的select查询来测试Hibernate,但它从来没有执行主文件。它可以一整天卡在:
INFO: HHH000397: Using ASTQueryTranslatorFactory
这是日志:
run: mai 03, 2016 8:58:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 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 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 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 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; } }
admin 更改状态以发布 2023年5月20日
首先,在日志中查看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(); Listprojets = session.createQuery("from Projet").list(); System.out.println(projets); session.close(); } finally { HibernateUtil.close(); } } }