0

I'm working on a project with Hibernate now but I get an error when running my project.

On server.java (my entry point) I got this:

private static HibernateUtil hibernateUtil;

And in my entry point:

hibernateUtil = new HibernateUtil();

This is my HibernateUtil class:

public class HibernateUtil {

    private SessionFactory sessionFactory;

    public HibernateUtil() {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();

        try {
            sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
        }
        catch (Exception e) {
            StandardServiceRegistryBuilder.destroy( registry );
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Now my Hibernate.cf.xml:

<hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dynamicdb</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="show_sql">false</property> 
    <property name="connection.pool_size">1</property>
</session-factory>

And now this is the error I get:

  Jun 18, 2016 1:17:17 PM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {5.2.0.Final}
  Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment <clinit>
  INFO: HHH000206: hibernate.properties not found
  Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  Jun 18, 2016 1:17:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
  INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/dynamicdb]
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  INFO: HHH10001001: Connection properties: {user=root, password=****}
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  INFO: HHH10001003: Autocommit mode: false
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
  INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
  Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
  INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/dynamicdb]
  Jun 18, 2016 1:17:18 PM org.hibernate.service.internal.AbstractServiceRegistryImpl stopService
  INFO: HHH000369: Error stopping service [class org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] : java.lang.NullPointerException

I'm running Eclipse mars 2 on Mac OS X 10.10 with MAMP. MySQL is running and the database user and database exists and the password is correct. What's the problem?

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • Define a connection pool : http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/hibernate-configuration/configure-a-connection-pool-with-hibernate – Guillaume F. Jun 18 '16 at 11:57
  • With : `catch (Exception e) {StandardServiceRegistryBuilder.destroy( registry );}` you may hide an exception, which could be useful for debugging. Could you at least log there the exception ? The Nullpointer we see at the end of the logs do not have a stacktrace ? – Thierry Jun 18 '16 at 13:47
  • also have a look at this question : http://stackoverflow.com/questions/16596528/hibernate-throwing-nullpointerexception – Thierry Jun 18 '16 at 13:50
  • please post the exception stack trace – Saravana Jun 19 '16 at 04:25

4 Answers4

0

I have the same problem like you. It seems that we both copy the code from Hibernate Official code samples. I think maybe it's bug of mysql connecotrj 6.0.After I add throw statement to the catch statement:

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
        .configure()
        .build();
try {
    sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
    StandardServiceRegistryBuilder.destroy(registry);
    throw new RuntimeException(e);
}

And I found the origin exception:

Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' 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.

This case doesn't appear in mysql connectorj 5.1

So, there are two solutions:switch mysql jdbc drivers to 5.1 or add timezone argument to jdbc url string like this. In hibernate.cfg.xml file don't forget to change & to &amp;

Community
  • 1
  • 1
yitian
  • 689
  • 1
  • 5
  • 11
0

I got the same issue when my mapping file contained one column twice

    <property column="STATE" name="state" type="java.lang.String" not-null="true"/>
    <property column="BRANCH" name="branch" type="java.lang.String" not-null="true"/>
    <property column="COUNTRY" name="country" type="java.lang.String" not-null="true"/>
    <property column="STATE" name="state" type="java.lang.String" not-null="true"/>

So once I changed one state to appropriate property e.g.

<property column="SITE" name="site" type="java.lang.String" not-null="true"/>

The issue was solved. So be careful with your mappings

0

In my case, I was using Netbeans and had my entities generated using Netbeans only.

after fiddling with mysql and hibernate-core jar versions, after commenting @NamedQueries fixed it for me.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
0

For others who may still encounter this issue, the database provided in the connection URL needs to exist, in this case, we need to ensure the database dynamicdb exists. I had the same issue and this fixed it.