简体   繁体   中英

Java hibernate throws Exception at boot

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?

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 ;

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 eg

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

The issue was solved. So be careful with your mappings

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM