问题描述
在尝试创建HibernateSession应用程序期间失败,并发生以下异常:
During attempt to create HibernateSession application fails with exception:
我在此处找到了一种可能的解决方案,但不幸的是,我没有访问数据库服务器的权限,因此我无法更改其配置.因此请注意,这不是重复的,因为针对数据库服务器更改提供了建议的解决方案,而在我的情况下,我没有这种访问权限.
I found one possible solution here but unfortunately I do not have access to DB server thus I cannot change its configuration.So please pay attention that this is not a duplicate, because suggested solution was provided for the DB server changes and in my case I don't have such access.
是否有机会在客户端解决此问题?在下面,您可以找到我创建会话的pom.xml文件和Java代码.
Is it any chance to fix this issue on client side? Below you can find my pom.xml file and java code where I create session.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
还有我的pom.xml:
And my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
推荐答案
一些进一步的研究表明,问题完全出在MySQL v.8.0中所做的更改:
Some further investigation showed that the issue was exactly in changes which were done in MySQL v.8.0:
重要更改:默认字符集具有 从latin1更改为utf8mb4.这些系统变量会受到影响:
Important Change: The default character set has changed from latin1 to utf8mb4. These system variables are affected:
character_set_server和 character_set_database系统变量已从 latin1 更改为 utf8mb4 .
The default value of the character_set_server and character_set_database system variables has changed from latin1 to utf8mb4.
collation_server的默认值和 collation_database系统变量已从 latin1_swedish_ci 更改 到 utf8mb4_0900_ai_ci .
The default value of the collation_server and collation_database system variables has changed from latin1_swedish_ci to utf8mb4_0900_ai_ci.
所有这些更改已在新版本的mysql-connector-java中处理,因此无需配置MySQL.因此,将5.1.6
更改为5.1.44
即可解决此问题:
All these change were already processed in new version of mysql-connector-java and there is no need to configure your MySQL. So change from 5.1.6
to 5.1.44
fix the issue:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
这篇关于从服务器收到未知的初始字符集索引"255"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!