Java hibernate在启动时抛出异常

时间:2016-06-18 11:26:14

标签: java hibernate

我现在正在使用Hibernate开发一个项目,但在运行项目时遇到错误。

在server.java(我的入口点)我得到了这个:

private static HibernateUtil hibernateUtil;

在我的切入点:

hibernateUtil = new HibernateUtil();

这是我的HibernateUtil类:

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

现在我的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>

现在这是我得到的错误:

  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

我在Mac OS X 10.10上使用MAMP运行Eclipse mars 2。 MySQL正在运行,数据库用户和数据库存在且密码正确。有什么问题?

4 个答案:

答案 0 :(得分:0)

我和你一样有同样的问题。看来我们都从Hibernate官方代码示例中复制了代码。我想也许是mysql connecotrj 6.0的bug。我把throw语句添加到catch语句后:

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

我发现原点例外:

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.

这种情况不会出现在mysql connectorj 5.1

因此,有两种解决方案:将mysql jdbc驱动程序切换为5.1或将时区参数添加到jdbc url字符串,如this。在hibernate.cfg.xml文件中不要忘记更改&amp;到&amp;

答案 1 :(得分:0)

当我的映射文件包含两列

时,我遇到了同样的问题
    <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"/>

所以,一旦我将一个州改为适当的属性,例如

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

问题解决了。 所以要小心你的映射

答案 2 :(得分:0)

就我而言,我使用的是Netbeans并且仅使用Netbeans生成了我的实体。

在摆弄了mysql和hibernate-core jar版本后,在评论@NamedQueries之后为我修复了它。

答案 3 :(得分:0)

对于可能仍然遇到此问题的其他人,需要存在连接URL中提供的数据库,在这种情况下,我们需要确保数据库dynamicdb存在。我遇到了同样的问题,并已解决。