好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

jpa @Column columnDefinition属性失效

删除一条属性,默认false

?

1

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

原因

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

例如

1.属性设置为true

?

1

2

alter table `xxx` add column `xxx` `varchar( 50 ) default '' `

// sql 语法错误

2.属性为false

?

1

2

alter table xxx add column xx varchar( 50 ) default ''

// 执行成功

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

jpa column注解

知识点

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

name :name属性定义了被标注字段在数据库表中所对应字段的名称; unique :unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。 nullable :nullable属性表示该字段是否可以为null值,默认为true。 insertable :insertable属性表示在使用[INSERT]脚本插入数据时,是否需要插入该字段的值。 updatable :updatable属性表示在使用[UPDATE]脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。 columnDefinition :columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。(也就是说,如果DB中表已经建好,该属性没有必要使用。) table :table属性定义了包含当前字段的表名。 length :length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。 precision 和 scale :precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。

precision和scale疑点

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

@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:

?

1

2

3

4

5

6

7

8

9

10

11

12

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数据库进行精确,则改为

?

1

2

@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类型中有效

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_35244529/article/details/87983785

查看更多关于解决springboot jpa @Column columnDefinition等属性失效问题的详细内容...

  阅读:19次