JPA设置默认字段及其长度详解⽬录
String
定义 Integer 和 Long 有区别吗
定义Integer和int有区别吗
boolean类型
⽇期
浮点数格式的设置
⼤⽂本
使⽤jpa去⽣成对应的值的长度和默认值是如何设置的呢
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String name() default "";
boolean unique() default false;
boolean nullable() default true;
varchar2最大长度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;
}
name 属性⽤来设置字段的名字
unique⽤于设置这个字段是否是是唯⼀的
insertable和updatable、table都是和表更新相关的操作,
length 指定长度,默认是255
precision 数据长度
scale⼩数的长度
columnDefinition 指定这⼀列的信息
String
string是最常见的字段,
@Column(name = “name”)
private String name;
SQL中 name varchar(255)
⽣成的字段长度为255,即如果不设置长度的话默认的长度就是255个。
@Column(name = “name”,length = 50)
private String name;
name varchar(50)
@Column(name = “name”,columnDefinition=“varchar(11) COMMENT ‘⽤户姓名'”)
private String name;
name varchar(11) COMMENT ‘⽤户姓名'
这⾥不仅指定了长度,还给列了⼀个注释,便于查看
@Column(name = “name”,columnDefinition=“default ‘12345'”)
private String name;
如果我们制定默认值,这样SQL语句就会报错
create table user (id bigint not null, age integer, birth datetime(6), name default ‘12345', sex bit, primary key (id)) engine=InnoDB
如果我们制定长度呢
@Column(name = “name”,columnDefinition=“default ‘12345'”,length = 25)
private String name;
运⾏的DDL语句依然⽆法创建出表
create table user (id bigint not null, age integer, birth datetime(6), name default ‘12345', sex bit, primary key (id)) engine=InnoDB
如何给String字段指定默认值呢
所以如果指定columnDefinition这个属性会覆盖原来的列注解的长度,⽽且在其注解的中必须制定列的类
/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p> Defaults to the generated SQL to create a
* column of the inferred type.
*/
String columnDefinition() default "";
/**
*(可选)在以下情况下使⽤的SQL⽚段:
*正在为列⽣成DDL。
*默认使⽤⽣成的SQL来创建推断类型的列。
*/
@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 会将其中的值作为列名之后,如果在这⾥设置默认值,必须保证直接加在列名之后执⾏不会出错。
定义 Integer 和 Long 有区别吗
Long的默认长度为20,Integer的默认长度为11
定义Integer和int有区别吗
区别是int如果你不给他设置就会默认为0
boolean和Boolean也是⼀样的。
boolean类型
默认长度为1
@Column(name = “sex”)
private Boolean sex;
@Column(name = “sex”)
private boolean sex;
⼆者没有什么区别,如果我们将boolean设置默认值并且设置长度会咋样呢?
@Column(name = “sex”,length = 50)
private boolean sex;
⽣成的DDL语句中并没有长度的属性。所以在boolean类型中设置长度属性是没有作⽤的
create table user (id bigint not null, age integer, birth datetime(6), name varchar(255), sex bit, primary key (id)) engine=InnoDB
指定boolean类型的默认值呢
@Column(name = “sex”,columnDefinition = “bit(1) default 1”)
private boolean sex;
有注意,如果指定默认值为1,即是true,属性如果不指定,boolean的默认值就是false,所以会将false设置到对应的数据中,如果指定Boolean呢
如果指定Boolean的话,没有设置默认就是null,就会将空更新到数据库中,最好⽤的⽅法还是在java类中设置默认值。⽇期
默认的⽇期格式不具有可读性
@Column(name = "birth")
private Date birth;
使⽤@Temporal来制定格式
@Temporal(TemporalType.DATE)
@Column(name = "birth")
private Date birth;
@Temporal有三个值
DATE 只有⽇期
TIME 只有时间时分秒
TIMESTAMP ⽇期和时间都有
浮点数格式的设置
@Column(name = “age”,scale = 2)
private int age;
age integer,属性为scale = 2没有作⽤
@Column(name = “age”,scale = 2)
private float age;
@Column(name = “age”,precision = 5,scale = 2)
private float age;
还是没有⽤,指定数据格式为double,依然没有⽤
@Column(name = “age”,columnDefinition = “decimal(5,2)”)
private double age;
⼤⽂本
/**
* 备注
*/
@Basic(fetch = FetchType.LAZY)
@Type(type = "text")
@Lob
@Column(name = "remark")
private String remark;
@Lob 指定该⽂本为长⽂本
@Basic(fetch = FetchType.LAZY) 长⽂本缓加载,便于数据的读取。
以上就是JPA设置默认字段及其长度详解的详细内容,更多关于JPA设置默认字段及长度的资料请关注其它相关⽂章!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。