[英]Java hibernate throws Exception at boot
I'm working on a project with Hibernate now but I get an error when running my project. 我现在正在使用Hibernate处理项目,但是运行项目时出现错误。
On server.java (my entry point) I got this: 在server.java(我的入口点)上,我得到了:
private static HibernateUtil hibernateUtil;
And in my entry point: 在我的入口点:
hibernateUtil = new HibernateUtil();
This is my HibernateUtil class: 这是我的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;
}
}
Now my Hibernate.cf.xml: 现在我的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. 我正在带有MAMP的Mac OS X 10.10上运行Eclipse mars 2。 MySQL is running and the database user and database exists and the password is correct. MySQL正在运行,并且数据库用户和数据库存在并且密码正确。 What's the problem? 有什么问题?
I have the same problem like you. 我有和你一样的问题。 It seems that we both copy the code from Hibernate Official code samples. 看来我们俩都从Hibernate Official代码示例中复制了代码。 I think maybe it's bug of mysql connecotrj 6.0.After I add throw statement to the catch statement: 我认为这可能是mysql connecotrj 6.0的bug。在catch语句中添加throw语句后:
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 这种情况在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 . 因此,有两种解决方案:开关MySQL的JDBC驱动程序到5.1或添加时区参数JDBC URL字符串像这样 。 In hibernate.cfg.xml file don't forget to change & to &
; 在hibernate.cfg.xml文件中,不要忘记将&更改为&
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. 就我而言,我使用的是Netbeans,而我的实体仅使用Netbeans生成。
after fiddling with mysql and hibernate-core jar versions, after commenting @NamedQueries fixed it for me. 摆弄mysql和hibernate-core jar版本后,在评论@NamedQueries后为我修复了它。
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. 对于可能仍然遇到此问题的其他人,需要存在连接URL中提供的数据库,在这种情况下,我们需要确保数据库dynamicdb存在。 I had the same issue and this fixed it. 我有同样的问题,并解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.