简体   繁体   English

尝试使用Hibernate创建MySQL连接时发生异常

[英]Exception when trying to create MySQL connection with Hibernate

I have been getting an exception while creating first ever application in hibernate. 在休眠状态下创建第一个应用程序时,我一直遇到异常。 I did search but the solutions provided didn't help me. 我确实进行了搜索,但是提供的解决方案并没有帮助我。 What odd I am doing here due to which I have burned my hours on this. 由于在这里我花了很多时间,所以我在这里做的事情真奇怪。

Please see the content below to better point out the issue. 请查看下面的内容以更好地指出问题。 使用所有库的项目结构

Student.java 学生.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 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 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 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();
    }
}

Yes, I have double checked that there is a username and password both root and I have also created db named school and port is 3306. 是的,我已经仔细检查了是否同时存在root用户名和密码,并且还创建了一个名为school的数据库,端口是3306。

Exception: 例外:

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

Tricky one! 整! The culprit seems to be how you configure your session factory. 罪魁祸首似乎是您配置会话工厂的方式。 The addResource() is for loading mappings, but silently ignores all the other settings. addResource()用于加载映射,但无提示地忽略所有其他设置。 Try this instead: 尝试以下方法:

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

Good luck with your project! 祝您项目顺利!

You didn't call configure method 您没有调用configure方法

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

add this it will work. 添加它会起作用。

also from hibernate 4, you can initialize with ServiceRegistry , have a look at the documentation 同样从休眠4开始,您可以使用ServiceRegistry进行初始化,看看文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate&Spring:尝试创建事务时出现异常 - Hibernate & Spring: Exception when trying to create a transaction 尝试使用休眠模式连接到mysql时连接被拒绝 - Connection refused when trying to connect to mysql using hibernate 创建表时休眠异常 - Hibernate exception when create table org.hibernate.exception.JDBCConnectionException:无法在MySQL中打开连接异常 - org.hibernate.exception.JDBCConnectionException: Could not open connection exception in MySQL 尝试创建数据库连接时出现异常 - Getting exception while trying to create a database connection 尝试创建 EntityManagerFactory 时线程“主”org.hibernate.service.spi.ServiceException 中的异常 - Exception in thread “main” org.hibernate.service.spi.ServiceException when trying to create EntityManagerFactory 尝试使用Java在Arango数据库中创建连接时出现“未经授权”异常 - Getting “Unauthorized” exception when trying to create connection in Arango database using Java 在Hibernate 3.2中使用连接池(c3p0-0.9.1.2)时,出现异常和应用程序无法与MySqL数据库连接? - Getting Exception and application not able to connect with MySqL Database when using connection pooling (c3p0-0.9.1.2) with Hibernate 3.2? 关闭与MySQL的连接时JDBC套接字异常 - JDBC Socket Exception when closing connection to MySQL 尝试建立与 MySQL 数据库的连接时出现 StackOverflowError - StackOverflowError when trying to establish connection to MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM