2

I want to create a new SessionFactory programatically with the following code:

    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);
    }
}

At the last line (SessionFactory.sessionFactory = conf.buildSessionFactory()) application throws:

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

Mysql server is working properly, log and password are OK. I checked .JARs and I think all is ok.

Thank you.

------- EDIT NEW DATA -------- I add Hibernate log output:

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

Once executed first line:

Configuration conf = new Configuration();

---------------- EDIT WITH 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

------- EDIT WITH 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>
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Joseph
  • 149
  • 1
  • 2
  • 9
  • What version of hibernate you are using? – user8271644 Jul 24 '17 at 14:37
  • I tryied with 5.2.10 – Joseph Jul 24 '17 at 16:02
  • Can you try this and see if it makes any difference ************ ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration .buildSessionFactory(serviceRegistry); – user8271644 Jul 24 '17 at 16:40
  • Thank you for your fast reply. I tried, and the same error at `sessionFactory = conf.buildSessionFactory(serviceRegistry); ` – Joseph Jul 24 '17 at 17:30
  • Please, check out new data about the problem. Than you user8271644 – Joseph Jul 24 '17 at 17:43
  • Do you have a file named hibernate.cfg.xml? – user8271644 Jul 24 '17 at 17:50
  • Also can you change the System.out.println("Error: " + excp) to System.out.println("Error: " + excp.printStackTrace()); Should give you the full stack trace and possibly the actual cause of the issue – user8271644 Jul 24 '17 at 17:57
  • Yes, I have a hibernate.cfg.xml. I removed it, clean + build, and the same error. I will update my post with stack trace. Thank you. – Joseph Jul 24 '17 at 18:03
  • is it under the scr/java/resources folder? – user8271644 Jul 24 '17 at 18:07
  • Yes, I had it there. But my intention is configure full programatically, so no needed of this file. I'm trying to remove any cofiguration file that could cause errors. – Joseph Jul 24 '17 at 18:09
  • 1
    Please try using a newer version of MYSQL------ mysql mysql-connector-java 6.0.3 – user8271644 Jul 24 '17 at 18:10
  • Done and changed for 6.0.3. Previous version was: 6.0.6. But same error. Thank you. Maybe any other dependency? – Joseph Jul 24 '17 at 18:15
  • Still getting the issue? – user8271644 Jul 24 '17 at 18:15
  • Thank you for your fast reply. Yes, same issue. :-(. I edit with pom.xml data. – Joseph Jul 24 '17 at 18:16
  • Did anything change in the stacktrace? – user8271644 Jul 24 '17 at 18:17
  • I checked carefully and I'm pretty sure there are no changes in stacktrace. I will try with other versions of mysql jar. – Joseph Jul 24 '17 at 18:22
  • Ok, for versions of mysql driver > 6 it doesnt work. I tried with any version of mysql 5 and under and it worked. Please, create a answer, your post Please try using a newer version of MYSQL------ mysql mysql-connector-java 6.0.3 has been the key to solve this problem. You deserve a great score. – Joseph Jul 24 '17 at 18:25

3 Answers3

3

Please try using a newer version of MYSQL or atleast changing your version of MYSQL jar

 <dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId> 
    <version>6.0.3</version> 
    </dependency> 
user8271644
  • 219
  • 2
  • 8
1

when formatting everything and reinstalling my computer, I did not notice the MySQL version and downloaded the last one available.

I made the clone of my project and started the mistakes. I got the same error and follow the steps suggested by Joseph.

I'll try to take a look here for the facts, to see if anyone can help.

This one was the first:

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

I thought for a moment, problem with the driver. Instead of connecting local "jdbc: mysql://localhost:3306/mydb" I tried connecting to another PC, changing the connection URL to "jdbc:mysql://192.168.1.5:3306/mydb" and I got the error :

Host is not allowed to connect to this mysql server

I was updating the mysql connector version. I started with 6.03, then 6.0.6, and so on. I was able to identify that this new version, uses a strong password system, according (https://github.com/dbeaver/dbeaver/issues/1042)

Since I was already installed, I opted to remove the root user and add it again, but via the command line and, using the default password system, according to https://www.spigotmc.org/threads/mysql-errors-unable -to-load-authentication-plugin-caching_sha2_password.304616 /

When updating the connector, I had to update the "hibernate.connection.driver_class" property to "com.mysql.cj.jdbc.Driver"

With this, a few more errors emerged:

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

I then upgraded maven to the latest version of the connector. He went on to present the error:

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

Following the Hibernate - ClassNotFoundException: com.mysql.jdbc.Driver, I added an external JAR.

Finally, even the timezone, gave problem

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.

With the hint here https://en.stackoverflow.com/questions/128873/erro-ao-execute-hibernate I changed the connection url and finally it worked.

Opera Summary:

Changed properties

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 ();
    }
}

External JAR added. [EDIT]After project run, I've removed the JAR and worked fine.[/EDIT]

E in POM.xml

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

So, it worked. I hope it can help someone.

0

It's a version compatible issue between MYSQL and C/J (mysql-connector-java)

Try to upgrade your C/J. You can refer here for the specific versions.

Chao Ma
  • 1
  • 1