码迷,mamicode.com
首页 > 编程语言 > 详细

springboot jpa

时间:2020-03-26 01:04:17      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:for   没有   factory   uml   user   The   drop   ber   实体类   

hibernate配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ZHPLUS?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.database = MYSQL
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.hbm2ddl.auto= update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.use-new-id-generator-mappings=false

1.使用新版本mysql驱动启动报错

注:java.sql.SQLException:

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 jdbc 6.0 版本以上必须配置“serverTimezone”参数;UTC代表的是全球标准时间,Asia/Shangha代表的是中国标准时间,领先UTC八个小时。

 

2.启动报错hibernate.dialect‘ not set

解决办法:

  Hibernate SQL方言没有设置导致的,在properties文件中增加

  spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect(低版本)

  spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect(高版本)

 

3.intellij idea Entity类报错“Cannot resolve table”

解决办法:idea的Database配置一下数据库

 

4. 没有根据实体类生成表

解决方法

注解的使用

@Entity
@Table(name = "t_user")

  application.properties中需配置:spring.jpa.properties.hibernate.hbm2ddl.auto=update

  有四个值:
  create: 每次加载 hibernate 时都会删除上一次的生成的表,然后根据你的 model 类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
  create-drop :每次加载 hibernate 时根据 model 类生成表,但是 sessionFactory 一关闭,表就自动删除。
  update:最常用的属性,第一次加载 hibernate 时根据 model 类会自动建立起表的结构(前提是先建立好数据库),以后加载 hibernate 时根据 model 类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服器  后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
  validate :每次加载 hibernate 时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
主键的注解

5.Mysql创建实体类后生成序列了

解决办法:
  方法一:
  将@GeneratedValue(strategy = GenerationType.AUTO)改为@GeneratedValue(strategy = GenerationType.IDENTITY)
  方法二:
  在application.properties文件中,增加spring.jpa.hibernate.use-new-id-generator-mappings= false

springboot jpa

标签:for   没有   factory   uml   user   The   drop   ber   实体类   

原文地址:https://www.cnblogs.com/zh1024/p/12571736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!