我是冬眠的新手。我目前正在使用Spring Boot框架并尝试通过Hibernate创建数据库表。
我以前知道同样的问题,但我似乎无法弄清楚如何根据我的环境解决错误。
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="connection_userid">user</property>
<property name="connection_pwd">pass</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection_pool_size">true</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">1</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbmdl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="com.test.springboot.model.AdultParticipant" />
</session-factory>
主类
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
AdultParticipant ap = new AdultParticipant();
ap.setFirstName("User");
ap.setLastName("UserLastName");
session.persist(ap);
t.commit();
session.close();
System.out.println("successfully saved");
pojo class
@Entity
@Table(name = "adultparticipant")
public class AdultParticipant {
@GeneratedValue
@Id
@Column (name = "id")
private int id;
@Column (name = "firstName")
private String firstName;
@Column (name = "lastName")
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
daoimpl类
public class AdultParticipantDAOImpl implements AdultParticipantDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void save(AdultParticipant ap) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.persist(ap);
tx.commit();
session.close();
}
@SuppressWarnings("unchecked")
@Override
public List<AdultParticipant> list() {
Session session = this.sessionFactory.openSession();
List<AdultParticipant> adultParticipants = session.createQuery("from AdultParticipant").list();
session.close();
return adultParticipants;
}
}
dao class
public interface AdultParticipantDAO {
public void save(AdultParticipant p);
public List<AdultParticipant> list();
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-springboot</artifactId>
<name>hello-springboot</name>
<description>hello-springboot</description>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
控制台中的错误
2017-03-13 11:48:40.512 WARN 9532 --- [ main] org.hibernate.orm.connections.pooling : HHH10001002: Using H
ibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate
.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
将mysql驱动程序升级到mysql-connector-java-8.0.17和
那些使用的人大于MySQL 5.5 版本
更改其驱动程序属性
来自 com.mysql.jdbc.Driver
com.mysql.cj.jdbc.Driver
因为:
加载类com.mysql.jdbc.driver。这是弃用的。新的 驱动程序类是com.mysql.cj.jdbc.driver。驱动程序自动 通过SPI注册和驾驶员类的手动加载是 通常不需要。Info -HHH000401:使用驱动程序 [com.mysql.jdbc.driver]在url ....
in hibernate.properties
hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
或如果您正在使用 hibernate.cfg.xml
UPDATE
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
connection.driver_class
属性必须与 mysql-connector
jar兼容。
mysql-connector
&lt;5.5.xx
使用com.mysql.jdbc.Driver
mysql-connector
> 5.5.xx
使用com.mysql.cj.jdbc.Driver
<property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
您需要设置serverTimezone
。
使用hsqldialect而不是mysqldialect
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/xyz?serverTimezone=CST6CDT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
您需要在依赖项中添加 mysql
jdbc jar。
- 将您的驱动程序类名称定为
com.mysql.jdbc.Driver
。 -
修复您的用户名和密码属性
"connection.username" for database user "connection.password" for database user password
-
创建MySQL数据库。看到这个。
-
对于SSL警告,请修改您的Connection.url以包括使用SSL false。例如,
jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false
-
将您的MySQL方言修改为
org.hibernate.dialect.MySQLDialect
- 使用
<property name="hbmdl.auto">create-drop</property>
代替<property name="hbmdl.auto">update</property>
,但是在生产中使用此选项。您应该自己创建模式,而不是通过Hibernate进行。
我在演示环境中将战争文件部署到tomcat时也遇到了同样的问题。我的应用程序没有入门,因为在我的/tomcat/webapp/root文件夹中,我在那里找到了战争文件myapp.war。即在我的根文件夹中,它具有Web-Inf,Meta-Inf和MyApp.war,这引起了启动问题。我删除了战争文件并起作用。
我也有同样的问题:我犯的错误是在属性之间定义了双线。因此,您必须非常小心以使主要连接属性正确。
其次,请确保指定的方言对于所使用的DB版本是正确的。要查看各种方言,您需要转到Hibernate-core-5.3.1.final.jar文件,然后从那里查找org.hibernate.dialect。列出了可用的各种方言。
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- property name="connection.driver_class">com.mysql.jdbc.Driver</property com.mysql.cj.jdbc.Driver -->
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">mypassword</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<!--<property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="org.test.connection.UserDetails" />
</session-factory>
</hibernate-configuration>