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 that the JPA default value setting has no effect

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "JPA default setting is not effective how to solve", in the daily operation, I believe that many people have doubts about how to solve the problem without the effect of JPA default setting. The editor consulted all kinds of materials and sorted out simple and useful methods of operation. I hope it will be helpful for you to answer the doubt that "JPA default setting is not effective". Next, please follow the editor to study!

Problem: default setting has no effect

There are two ways to add default values to the class of the JPA database table of the springboot project

1. @ Column (columnDefinition = "varchar (35) default '12345'")

2. @ ColumnDefault ("12345")

Requirement: set the default value of img field to / imgs/defunct.png

Dependence

Org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java 5.1.46 org.projectlombok lombok 1.16.20

Mapping class

Data@NoArgsConstructor@AllArgsConstructor@DynamicInsert@DynamicUpdate@Entity (name = "tbl_user") public class UserInfo {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) @ ColumnDefault ("\\ imgs\\ defunct.png") private String img;@ColumnDefault ("0") private Integer studyBegin;}

Database design table field

Only the default setting of the studyBegin default value to 0recoverimg is unsuccessful.

Solution.

Put

@ ColumnDefault ("/ imgs/defunct.png")

Change to

@ Column (columnDefinition = "varchar (255) default'/ imgs/defunct.png'")

Effect.

JPA sets default values for fields

When using JPA, if you need to set default values for properties, naturally, you may want to use the following ways.

@ Column (name= "state", columnDefinition= "tinyint default 0") private Integer state=0

Unfortunately, this solution is sometimes not feasible because columnDefinition is used when creating tables if your tables are not automatically generated.

Then we need to add the default value for the attribute, what should we do? in fact, it is very simple to assign a value to the attribute directly.

Private Integer state=0; at this point, on the "JPA default setting is not effective how to solve" the end of the study, I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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