我目前正在学习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!
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!");
}
}
类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;
}
}
答案 0 :(得分: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();
}
}
}