2016-06-18 9 views
0

Ich arbeite jetzt an einem Projekt mit Hibernate, aber ich bekomme einen Fehler, wenn ich mein Projekt ausführen.Java Hibernate Throws Ausnahme beim Start

Auf server.java (mein Einstiegspunkt) habe ich dies:

private static HibernateUtil hibernateUtil; 

Und in meinem Einstiegspunkt:

hibernateUtil = new HibernateUtil(); 

Das ist mein HibernateUtil Klasse:

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

Jetzt mein 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> 

Und jetzt dies ist der Fehler, den ich bekommen:

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 

ich Eclipse-Mars 2 auf Mac OS X 10.10 mit MAMP renne. MySQL läuft und der Datenbankbenutzer und die Datenbank existieren und das Passwort ist korrekt. Was ist das Problem?

+0

Definieren Sie einen Verbindungspool: http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/hibernate-configuration/configure-a-connection-pool-with-hibernate –

+0

Mit: 'catch (Exception e) {StandardServiceRegistryBuilder.destroy (registry);} 'Sie können eine Ausnahme verbergen, die für das Debuggen nützlich sein könnte. Kannst du da wenigstens die Ausnahme melden? Der Nullpointer, den wir am Ende der Logs sehen, hat keinen Stacktrace? – Thierry

+0

werfen Sie auch einen Blick auf diese Frage: http: // Stackoverflow.com/questions/16596528/hibernate-wowing-nullpointerexception – Thierry

Antwort

0

Ich habe das gleiche Problem wie du. Es scheint, dass wir beide den Code von Hibernate Official Code Samples kopieren. Ich denke, vielleicht ist es Fehler von MySQL connecotrj 6.0.After I throw-Anweisung an die catch-Anweisung hinzufügen:

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

Und ich den Ursprung Ausnahme gefunden:

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. 

Dieser Fall wird nicht in mysql connectorj 5.1

So gibt es zwei Lösungen: Switch mysql jdbc Treiber auf 5.1 oder Zeitzone Argument zu jdbc URL-String wie this hinzufügen. Vergessen Sie in der Datei hibernate.cfg.xml nicht, & zu &amp zu ändern;

0

habe ich das gleiche Problem, wenn meine Mapping-Datei

<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 zweimal eine Spalte enthalten, wenn ich einen Zustand in entsprechender Eigenschaft geändert z.B.

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

Das Problem wurde gelöst. Seien Sie also vorsichtig mit Ihren Zuordnungen

0

In meinem Fall verwendete ich Netbeans und ließ meine Entitäten nur mit Netbeans generieren.

nach fiedeln mit mysql und hibernate-core jar-version, nach dem kommentieren @namedQueries reparierte es für mich.