In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to set default fields and their lengths in JPA. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.
How to use jpa to generate the corresponding value length and default value
@Target({ElementType.METHOD, ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)public @interface Column { String name() default ""; boolean unique() default false; boolean nullable() default true; boolean insertable() default true; boolean updatable() default true; String columnDefinition() default ""; String table() default ""; int length() default 255; int precision() default 0; int scale() default 0;}
The name property is used to set the name of the field
unique is used to set whether this field is unique
insertable, updatable and table are operations related to table updates
length Specifies the length, the default is 255
precision data length
Scale The length of a fraction
columnDefinition Specifies the information for this column
String
string is the most common field,
@Column(name = "name")private String name;
SQL name varchar(255)
The generated field length is 255, that is, if the length is not set, the default length is 255.
@Column(name = "name",length = 50)private String name;name varchar(50)
@Column(name = "name",columnDefinition="varchar(11) COMMENT 'user name'")private String name;name varchar(11) COMMENT ' user name'"
Not only is the length specified here, but a comment is also listed for easy viewing
@Column(name = "name",columnDefinition="default '12345'")private String name;
If we set default values, SQL statements will report errors
create table user (id bigint not null, age integer, birth datetime(6), name default '12345', sex bit, primary key (id)) engine=InnoDB
What if we set the length?
@Column(name = "name",columnDefinition="default '12345'",length = 25)private String name;
Running DDL statements still fail to create tables
create table user (id bigint not null, age integer, birth datetime(6), name default '12345', sex bit, primary key (id)) engine=InnoDB
How to specify default values for String fields
So if you specify columnDefinition, this property overrides the length of the original column annotation, and the column type must be specified in its annotation.
/** * (Optional) The SQL fragment that is used when * generating the DDL for the column. *
Defaults to the generated SQL to create a * column of the inferred type. */ String columnDefinition() default "";/** * (Optional) SQL fragment to use when: * DDL is being generated for columns. * By default generated SQL is used to create columns of inferred type. */@ Column(name = "name",columnDefinition=" varchar(11) default '12345'",length = 25)private String name;create table user (id bigint not null, age integer, birth datetime(6), name varchar(11) default '12345', sex bit, primary key (id)) engine=InnoDB
ColumnDefinition uses the values in it as column names. If you set the default value here, you must make sure that you can execute it without errors directly after column names.
Is there a difference between Integer and Long?
The default length of Long is 20, and the default length of Integer is 11.
Is there a difference between Integer and int?
The difference is int, if you don't set it, it defaults to 0.
Boolean and Boolean are the same.
boolean type
Default length is 1
@Column(name = "sex")private Boolean sex;
@Column(name = "sex")private boolean sex;
There is no difference between the two, what if we set boolean to default and set the length?
@Column(name = "sex",length = 50)private boolean sex;
There is no length attribute in the generated DDL statement. So setting the length property in boolean has no effect
create table user (id bigint not null, age integer, birth datetime(6), name varchar(255), sex bit, primary key (id)) engine=InnoDB
Specify the default value for boolean type
@Column(name = "sex",columnDefinition = "bit(1) default 1")private boolean sex;
Note that if the default value is specified as 1, it is true. If the attribute is not specified, the default value of boolean is false, so false will be set to the corresponding data. If Boolean is specified,
If Boolean is specified, the default is null if not set, and null will be updated to the database. The best way to use it is to set the default value in java class.
date
The default date format is not readable
@Column(name = "birth")private Date birth;
Format using @Temporal
@Temporal(TemporalType.DATE) @Column(name = "birth") private Date birth;
@Temporal has three values
DATE Date only
TIME Time is only hours and minutes
TIMESTAMP Date and time available
@Column(name = "age",scale = 2)private int age;
age integer, attribute scale = 2 has no effect
@Column(name = "age",scale = 2)private float age;
@Column(name = "age",precision = 5,scale = 2)private float age;
Still not used, specified data format is double, still not used
@Column(name = "age",columnDefinition = "decimal(5,2)")private double age;
large text /** * remarks */ @Basic(fetch = FetchType.LAZY) @Type(type = "text") @Lob @Column(name = "remark") private String remark;
@Lob specifies that the text is long
@Basic(fetch = FetchType.LAZY) Long text loaded slowly for easy data reading.
The above is how JPA sets default fields and their lengths. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.