如何解决springboot jpa@Column columnDefinition等属性失效问题

发布时间:2021-10-25 10:07:52 作者:iii
来源:亿速云 阅读:258

本篇内容主要讲解“如何解决springboot jpa@Column columnDefinition等属性失效问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决springboot jpa@Column columnDefinition等属性失效问题”吧!

jpa @Column columnDefinition属性失效

删除一条属性,默认false

#spring.jpa.properties.hibernate.globally_quoted_identifiers=true

原因

开启后, 创建sql语句执行时会添加'`', 会造成columnDefinition 属性失效, author: dreamlu

例如

1.属性设置为true

alter table `xxx` add column `xxx` `varchar(50) default ''`
// sql 语法错误

2.属性为false

alter table xxx add column xx varchar(50) default ''
// 执行成功

可以看出: 有舍有得,第二种要求字段/表等命名不能和mysql或其他数据库中关键字重名

jpa column注解

知识点

@Column注解一共有10个属性,这10个属性均为可选属性,各属性含义分别如下:

precision和scale疑点

@Table(name = "CUSTOMERS")
@Entity
public class Customer {
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Integer id; 
    @Column(name = "Name")
    private String name;
 
    @Column(name = "Email", nullable = true, length = 128)
    private String email;
 
    @Column(name = "Age")
    private int age;
 
    @Column(name = "Remark", columnDefinition = "text")
    private String remark;
 
    @Column(name = "Salary1", columnDefinition = "decimal(5,2)")
    private double salary1;
 
    @Column(name = "Salary2", precision = 5, scale = 2)
    private double salary2;
 
    @Column(name = "Salary3", columnDefinition = "decimal(5,2)")
    private BigDecimal salary3;
 
    @Column(name = "Salary4", precision = 5, scale = 2)
    private BigDecimal salary4;
    ......
}

数据库DDL:

CREATE TABLE `customers` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Age` int(11) DEFAULT NULL,
  `Email` varchar(128) DEFAULT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `Remark` text,
  `Salary1` decimal(5,2) DEFAULT NULL,
  `Salary2` double DEFAULT NULL,
  `Salary3` decimal(5,2) DEFAULT NULL,
  `Salary4` decimal(5,2) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

小结一下

1.double类型若在columnDefinition属性中指定数字类型为decimal并指定精度,则最终以columnDefinition为准 (oracle数据库中除外,其指定为float类型,因为oracle数据库没有double类型,若针对oracle数据库进行精确,则改为

@Column(name = "Salary1", columnDefinition = "decimal(5,2)")  //或columnDefinition = "number(5,2)"
    private Float salary1;

2.double类型将在数据库中映射为double类型,precision和scale属性无效

3.BigDecimal类型在数据库中映射为decimal类型,precision和scale属性有效

4.precision和scale属性只在BigDecimal类型中有效

到此,相信大家对“如何解决springboot jpa@Column columnDefinition等属性失效问题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何用centos7.7搭建EFK7
  2. calico 2.6.1 升级至 3.11

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springbootjpa

上一篇:如何学习和理解Linux命令

下一篇:Python爬虫经常会被封的原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》