• JPA注解实体类,给表添加创建时间,更新时间,id的生成以及创建唯一约束


    首先创建一个BaseModel,自动生成创建时间和更新时间

    @SuppressWarnings("serial")
    @MappedSuperclass
    public class BaseModel implements Serializable{
    
        @Temporal(TemporalType.TIMESTAMP)
        @Column(insertable=false, updatable=false)
        @CreationTimestamp
        protected Date createTime;
        
        @JsonIgnore
        @Temporal(TemporalType.TIMESTAMP)
        @Column(insertable=false)
        @UpdateTimestamp
        protected Date lastUpdateTime;
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getLastUpdateTime() {
            return lastUpdateTime;
        }
    
        public void setLastUpdateTime(Date lastUpdateTime) {
            this.lastUpdateTime = lastUpdateTime;
        }
        
    }

    然后创建一个RandomIdModel,利用org.hibernate.id.UUIDHexGenerator生成的uuid作为主键

    @SuppressWarnings("serial")
    @MappedSuperclass
    public class RandomIdModel extends BaseModel {
    
        @Id
        @Column(length=32)
        @GeneratedValue(generator="UUIDHexGenerator")
        @GenericGenerator(name="UUIDHexGenerator", strategy="uuid")
        protected String id;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
        
    }

    然后就可以创建其他的具体实体类继承上面的model了,这样就保证了所有的表都会有创建时间和更新时间了

    @SuppressWarnings("serial")
    @Entity
    @Table(name="tb_user",
            indexes={ @Index(name=User.UK_NAME, unique=true, columnList="name"),
                    @Index(name=User.UK_PHONE, unique=true, columnList="phone")})
    public class User extends RandomIdModel{
    
        public static final String UK_NAME = "uk_name";
        public static final String UK_PHONE = "uk_phone";
        
        @Column(length=24)
        private String name;
        
        @Column(length=32)
        private String password;
        
        @Temporal(TemporalType.TIMESTAMP)
        private Date birth;
        
        @Enumerated(EnumType.STRING)
        @Column(length=8)
        private Sex sex;
        
        @Column(length=64)
        private String email;
        
        @Column(length=11)
        private String phone;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public Date getBirth() {
            return birth;
        }
        public void setBirth(Date birth) {
            this.birth = birth;
        }
        
        public Sex getSex() {
            return sex;
        }
        public void setSex(Sex sex) {
            this.sex = sex;
        }
        
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
    }

    创建唯一索引时,自定义索引名,有利于后续业务判断。

    当插入数据不唯一时,可根据抛出的异常,明确知道是哪个字段违反约束导致的

    if(e.getCause() instanceof ConstraintViolationException){
        String constraintName = ((ConstraintViolationException)e.getCause()).getConstraintName();
        if(constraintName.equals(User.UK_NAME)){
            return Resp.error(Resp.ErrorCode.USER_NAME_EXITS, null);
        }
        if(constraintName.equals(User.UK_PHONE)){
            return Resp.error(Resp.ErrorCode.USER_PHONE_EXITS, null);
        }
    }
  • 相关阅读:
    Python Module_Socket_网络编程
    Python Module_Socket_网络编程
    从 2017 OpenStack Days China 看国内云计算的发展现状
    从 2017 OpenStack Days China 看国内云计算的发展现状
    说说excel
    一种防脱裤撞库的可能性?
    黑白相片变彩色相片的一种可能性?
    为什么需求文档一定要电子化?
    一个网页布局练习
    css田字格布局
  • 原文地址:https://www.cnblogs.com/wenhui92/p/7594577.html
Copyright © 2020-2023  润新知