2016-06-11 70 views
0

在Hibernate中创建第一个应用程序时,我一直收到异常。我做了搜索,但提供的解决方案并没有帮助我。我在这里做了什么奇怪的事情,因此我在这里烧了几个小时。尝试与Hibernate创建MySQL连接时发生异常

请参阅下面的内容以更好地指出问题。 Project Structure with all libraries being used

Student.java

package beans; 

public class Student { 
    private int studentId; 
    private String name; 
    private int marks; 
    public int getStudentId() { 
     return studentId; 
    } 
    public void setStudentId(int studentId) { 
     this.studentId = studentId; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public int getMarks() { 
     return marks; 
    } 
    public void setMarks(int marks) { 
     this.marks = marks; 
    } 


} 

的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/school</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">root</property> 

     <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
     <property name="hbm2dll.auto">create</property> 
     <property name="show_sql">true</property> 

     <mapping resource="resources/Student.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

Student.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="beans.Student" table="student" schema="school"> 
     <id name="studentId"/> 
     <property name="name"/> 
     <property name="marks"/> 
    </class> 
</hibernate-mapping> 

Test.java

package test; 

import org.hibernate.cfg.Configuration; 

public class Test { 
    public static void main(String[] args) { 
     Configuration conf = new Configuration(); 
     conf.addResource("resources/hibernate.cfg.xml").buildSessionFactory(); 
    } 
} 

是的,我有双重检查,有一个用户名和密码根和我也分贝命名学校和端口创建为3306

例外:

WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections 
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723) 
    at test.Test.main(Test.java:8) 
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) 
    ... 14 more 
+0

你能或许与我们共享的例外呢? –

+0

我刚刚分享了这个例外。请建议。谢谢@PerHuss –

回答

1

你没有打电话给configure方法

Configuration conf = new Configuration().configure(); 

添加此它将工作。

也从休眠4,你可以用ServiceRegistry初始化,看看文档

+0

谢谢,这解决了我的问题。 –

1

整蛊一个!罪魁祸首似乎是你如何配置你的会话工厂。 addResource()用于加载映射,但默默忽略所有其他设置。试试这个:

conf.configure(new File("resources/hibernate.cfg.xml")) 
    .buildSessionFactory(); 

祝您的项目顺利!

相关问题