jpa @Column columnDefinition屬性失效
刪除一條屬性,默認(rèn)false
1
|
#spring.jpa.properties.hibernate.globally_quoted_identifiers= true |
原因
開(kāi)啟后, 創(chuàng)建sql語(yǔ)句執(zhí)行時(shí)會(huì)添加'`', 會(huì)造成columnDefinition 屬性失效, author: dreamlu
例如
1.屬性設(shè)置為true
1
2
|
alter table `xxx` add column `xxx` `varchar( 50 ) default '' ` // sql 語(yǔ)法錯(cuò)誤 |
2.屬性為false
1
2
|
alter table xxx add column xx varchar( 50 ) default '' // 執(zhí)行成功 |
可以看出: 有舍有得,第二種要求字段/表等命名不能和mysql或其他數(shù)據(jù)庫(kù)中關(guān)鍵字重名
jpa column注解
知識(shí)點(diǎn)
@Column注解一共有10個(gè)屬性,這10個(gè)屬性均為可選屬性,各屬性含義分別如下:
-
name
:name屬性定義了被標(biāo)注字段在數(shù)據(jù)庫(kù)表中所對(duì)應(yīng)字段的名稱; -
unique
:unique屬性表示該字段是否為唯一標(biāo)識(shí),默認(rèn)為false。如果表中有一個(gè)字段需要唯一標(biāo)識(shí),則既可以使用該標(biāo)記,也可以使用@Table標(biāo)記中的@UniqueConstraint。 -
nullable
:nullable屬性表示該字段是否可以為null值,默認(rèn)為true。 -
insertable
:insertable屬性表示在使用“INSERT”腳本插入數(shù)據(jù)時(shí),是否需要插入該字段的值。 -
updatable
:updatable屬性表示在使用“UPDATE”腳本插入數(shù)據(jù)時(shí),是否需要更新該字段的值。insertable和updatable屬性一般多用于只讀的屬性,例如主鍵和外鍵等。這些字段的值通常是自動(dòng)生成的。 -
columnDefinition
:columnDefinition屬性表示創(chuàng)建表時(shí),該字段創(chuàng)建的SQL語(yǔ)句,一般用于通過(guò)Entity生成表定義時(shí)使用。(也就是說(shuō),如果DB中表已經(jīng)建好,該屬性沒(méi)有必要使用。) -
table
:table屬性定義了包含當(dāng)前字段的表名。 -
length
:length屬性表示字段的長(zhǎng)度,當(dāng)字段的類型為varchar時(shí),該屬性才有效,默認(rèn)為255個(gè)字符。 -
precision
和scale
:precision屬性和scale屬性表示精度,當(dāng)字段類型為double時(shí),precision表示數(shù)值的總長(zhǎng)度,scale表示小數(shù)點(diǎn)所占的位數(shù)。
precision和scale疑點(diǎn)
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; ...... } |
數(shù)據(jù)庫(kù)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; |
小結(jié)一下
1.double類型若在columnDefinition屬性中指定數(shù)字類型為decimal并指定精度,則最終以columnDefinition為準(zhǔn) (oracle數(shù)據(jù)庫(kù)中除外,其指定為float類型,因?yàn)閛racle數(shù)據(jù)庫(kù)沒(méi)有double類型,若針對(duì)oracle數(shù)據(jù)庫(kù)進(jìn)行精確,則改為
1
2
|
@Column (name = "Salary1" , columnDefinition = "decimal(5,2)" ) //或columnDefinition = "number(5,2)" private Float salary1; |
2.double類型將在數(shù)據(jù)庫(kù)中映射為double類型,precision和scale屬性無(wú)效
3.BigDecimal類型在數(shù)據(jù)庫(kù)中映射為decimal類型,precision和scale屬性有效
4.precision和scale屬性只在BigDecimal類型中有效
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_35244529/article/details/87983785