Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of attribute invalidation such as springboot jpa@Column columnDefinition

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is "how to solve the problem of attribute invalidation such as springboot jpa@Column columnDefinition". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to solve the problem of attribute invalidation such as springboot jpa@Column columnDefinition".

The jpa @ Column columnDefinition attribute is invalid

Delete an attribute. Default is false.

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

When enabled, the'`'will be added when the sql statement is created, which will invalidate the columnDefinition attribute. Author: dreamlu

For example

1. Property is set to true

Alter table `xxx` add column `xxx`` varchar (50) default''`/ / sql syntax error

two。 Property is false

Alter table xxx add column xx varchar (50) default''/ / executed successfully

It can be seen that the second requirement is that fields / tables and other names should not be the same as keywords in mysql or other databases.

Jpa column annotated knowledge points

The @ Column annotation has a total of 10 attributes, all of which are optional. The meanings of each attribute are as follows:

The name:name attribute defines the name of the field corresponding to the annotated field in the database table.

The unique:unique property indicates whether the field is a unique identity, and the default is false. If there is a field in the table that needs to be uniquely identified, you can use either that tag or @ UniqueConstraint in the @ Table tag.

The nullable: nullable attribute indicates whether the field can be a null value, which defaults to true.

The insertable: insertable attribute indicates whether the value of this field needs to be inserted when inserting data using the "INSERT" script.

The updatable:updatable property indicates whether the value of the field needs to be updated when you insert data using the "UPDATE" script. Insertable and updatable properties are generally used for read-only properties, such as primary keys and foreign keys. The values of these fields are usually generated automatically.

The columnDefinition: columnDefinition attribute represents the SQL statement created by this field when the table is created, which is typically used when generating table definitions through Entity. (that is, if the table has been built in DB, this property is not necessary. )

The table: table attribute defines the name of the table that contains the current field.

The length: length attribute, which represents the length of the field, is valid only when the field is of type varchar, which defaults to 255characters.

The precision and scale: precision and scale attributes represent precision, and when the field type is double, precision represents the total length of the numeric value and scale represents the number of digits occupied by the decimal point.

Precision and scale doubt @ Table (name = "CUSTOMERS") @ Entitypublic 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 (5jue 2)") private double salary1; @ Column (name = "Salary2", precision = 5, scale = 2) private double salary2; @ Column (name = "Salary3", columnDefinition = "decimal (5jue 2)") private BigDecimal salary3; @ Column (name = "Salary4", precision = 5, scale = 2) private BigDecimal salary4 .}

Database DDL:

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

1.double type if the numeric type is decimal and precision is specified in the columnDefinition attribute, the final columnDefinition prevails (except in the oracle database, which is specified as the float type, because the oracle database does not have the double type, if it is accurate for the oracle database, it will be changed to

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

2.double types will be mapped to double types in the database, and the precision and scale properties are invalid

3.BigDecimal types are mapped to decimal types in the database, and the precision and scale attributes are valid

The 4.precision and scale properties are valid only in BigDecimal types

At this point, I believe you have a deeper understanding of "how to solve the problem of attribute invalidation such as springboot jpa@Column columnDefinition". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report