Loading

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);
    }
}

 

posted @ 2017-09-25 23:46  辉灬  阅读(3734)  评论(0编辑  收藏  举报