以编程方式配置Hibernate - 无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

时间:2017-07-24 14:28:39

标签: java mysql hibernate

我想用以下代码以编程方式创建一个新的SessionFactory:

    private static void createSessionFactory() {
    try {
        Configuration conf = new Configuration();
        conf.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        conf.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/database?zeroDateTimeBehavior=convertToNull");
        conf.setProperty("hibernate.connection.username", "root");
        conf.setProperty("hibernate.connection.password", "toor");
        conf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        conf.setProperty("hibernate.show_sql", "false");
        conf.setProperty("hibernate.hbm2ddl.auto", "update");
        conf.setProperty("hibernate.current_session_context_class", "thread");
//Added 2 annotated classes.
        conf.addAnnotatedClass(webtest.Category.class)
                .addAnnotatedClass(webtest.Question.class)
        SessionFactory.sessionFactory = conf.buildSessionFactory();
    } catch (HibernateException excp) {
        System.out.println("Error: " + excp);
    }
}

在最后一行( SessionFactory.sessionFactory = conf.buildSessionFactory())应用程序抛出:

Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Mysql服务器运行正常,日志和密码都可以。我查了.JARs,我觉得一切都好。

谢谢。

-------编辑新数据-------- 我添加了Hibernate日志输出:

jul 24, 2017 7:41:08 PM org.hibernate.Version logVersion INFO: HHH000412: 
Hibernate Core {5.2.10.Final} jul 24, 2017 7:41:08 PM  
org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties 
not found

执行第一行后:

Configuration conf = new Configuration();

----------------使用STACKTRACE进行编辑----------

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
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:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    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:242)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    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 bbddgestor.SessionFactory.createSessionFactory(SessionFactory.java:52)
    at bbddgestor.SessionFactory.<clinit>(SessionFactory.java:19)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at bbddgestor.BBDDController.openSession(BBDDController.java:30)
    at main.Main.JSONCONSULTADOR(Main.java:52)
    at main.Main.main(Main.java:47)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:105)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    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:259)
    ... 19 more
Caused by: java.sql.SQLException: The server time zone value 'Hora de verano romance' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:489)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:69)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1606)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:219)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
    ... 34 more
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'Hora de verano romance' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
    at com.mysql.cj.jdbc.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:118)
    at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:293)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2399)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1739)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1596)
    ... 38 more

-------用POM.XML编辑-------

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.webtest</groupId>
    <artifactId>GestorBBDD</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

        <dependency> 
            <groupId>mysql</groupId> 
            <artifactId>mysql-connector-java</artifactId> 
            <version>6.0.3</version> 
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.8.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-osgi</artifactId>
            <version>5.0.8.Final</version>
        </dependency>
    </dependencies>

</project>

3 个答案:

答案 0 :(得分:1)

请尝试使用较新版本的MYSQL或至少更改您的MYSQL jar版本

 <dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>6.0.3</version> 
    </dependency> 

答案 1 :(得分:1)

格式化所有内容并重新安装我的计算机时,我没有注意到MySQL版本并下载了最后一个版本。

我完成了项目的克隆并开始犯错误。我得到了同样的错误,并按照约瑟夫建议的步骤。

我试着看看这些事实,看看是否有人可以提供帮助。

这是第一个:

Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver # connect

我想了一下,司机的问题。而不是连接本地&#34; jdbc:mysql:// localhost:3306 / mydb &#34;我尝试连接到另一台PC,将连接URL更改为&#34; jdbc:mysql://192.168.1.5:3306 / mydb &#34;我收到了错误:

Host is not allowed to connect to this mysql server

我正在更新mysql连接器版本。我从6.03开始,然后是6.0.6,依此类推。我能够确定这个新版本使用强密码系统,根据(https://github.com/dbeaver/dbeaver/issues/1042

由于我已经安装,我选择删除root用户并再次添加,但是通过命令行,并使用默认密码系统,根据https://www.spigotmc.org/threads/mysql-errors-unable -to-load-authentication-plugin- caching_sha2_password.304616 /

更新连接器时,我不得不更新&#34; hibernate.connection.driver_class&#34;属性为&#34; com.mysql.cj.jdbc.Driver&#34;

有了这个,还出现了一些错误:

Caused by: java.sql.SQLException: Unknown system variable 'query_cache_size'

然后我将maven升级到最新版本的连接器。他继续提出错误:

Caused by: java.lang.ClassNotFoundException: Could not load requested class: com.mysql.jdbc.Driver

Hibernate - ClassNotFoundException: com.mysql.jdbc.Driver之后,我添加了一个外部JAR。

最后,即使是时区,也给出了问题

The server time zone value 'official time of Brazil' is unrecognized or represents more than one time zone. You must configure either the server or the JDBC driver (via the serverTimezone configuration property) to use a specific time zone value if you want to use time zone support.

在这里提示https://en.stackoverflow.com/questions/128873/erro-ao-execute-hibernate我改变了连接URL,最后它起作用了。

Opera摘要:

更改了属性

public class HibernateConnector {

    private static HibernateConnector me;
    private Configuration cfg;
    private SessionFactory sessionFactory;

    private HibernateConnector () throws HibernateException {

        cfg = new Configuration ();
      
        cfg.setProperty ("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver");
        cfg.setProperty ("hibernate.connection.url", "jdbc: mysql: // localhost: 3306 / mydatabase? useTimezone = true & serverTimezone = UTC");
        cfg.setProperty ("hibernate.connection.username", "root");
        cfg.setProperty ("hibernate.connection.password", "myPassword");
        cfg.setProperty ("hibernate.show_sql", "true");
        cfg.setProperty ("hibernate.hbm2ddl.auto", "update");

        cfg.addAnnotatedClass (myPackage.MyClass.class);

        sessionFactory = cfg.buildSessionFactory ();
    }
}

添加了外部JAR。 [编辑]项目运行后,我已经删除了JAR并且工作正常。[/ EDIT]

POM.xml中的E

<dependency>
<groupId> mysql </groupId>
<artifactId> mysql-connector-java </artifactId>
<version> 8.0.11 </version>
</dependency>

所以,它奏效了。 我希望它可以帮助别人。

答案 2 :(得分:0)

这是MYSQL和C / J之间的版本兼容问题(mysql-connector-java)

尝试升级您的C / J.您可以参考here了解具体版本。