JPA及部分Hibernate注解学习

(1)JPA & Hibernate的[字段相关注解]添加在[实体类字段的get方法]上

(2)dodo的[字段相关的注解]添加在[实体类的字段]上

学习这几个JPA & Hibernate注解,足够了

@DynamicInsert

org.hibernate.annotations.DynamicInsert

注解在实体类上,默认为true,设置为true,表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中。

@OneToOne

javax.persistence.OneToOne
// 例子,一个城市唯一归属一个省份
// 那么城市和省份是一对一(OneToOne)的关系
@DynamicInsert
public class City extends BaseEntity{
    private Province          province;

    @OneToOne(fetch = FetchType.LAZY)
    public Province getProvince() {
        return province;
    }
}

@OneToMany & @ManyToOne

javax.persistence.OneToMany
javax.persistence.ManyToOne
// 例子,一个实体类(Entity)包含多个字段(Field)
// 实体类和字段是一对多(OneToMany)的关系
// 字段和实体类时多对一(ManyToOne)的关系
@DynamicInsert
public class Entity extends BaseEntity{
    private Set<Field>        allFields;

    @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "entity")
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("sortSeq asc")
    public Set<Field> getAllFields() {
        return allFields;
    }
}

@DynamicInsert
public class Field extends BaseEntity{
    private Entity            entity;

    @ManyToOne
    public Entity getEntity() {
        return entity;
    }
}

@ManyToMany

javax.persistence.ManyToMany
// 例子,一个角色(Role)包含多个权限(Right)
//      一个权限(Right)也可以被多个角色(Role)引用
// 角色和权限是(ManyToMany)的关系
@DynamicInsert
public class Role extends BaseEntity {
    private Set<Right>          allRights;

    @ManyToMany(fetch = FetchType.LAZY)
    @OrderBy("sortSeq asc")
    public Set<Right> getAllRights() {
        return allRights;
    }
}

@DynamicInsert
public class Right extends BaseEntity{
    private Set<Role>         roles;

    // 指定由角色表来维护关系,则roles无须加@DodoField注解
    @ManyToMany(mappedBy = "allRights", fetch = FetchType.LAZY)
    public Set<Role> getRoles() {
        return roles;
    }
}

@Column

一般用来配置,字段的非空、长度、索引、精度以及范围
    // 设置字段长度128
    @Column(length = 128)
    public String getRightRemark() {
        return rightRemark;
    }

    // 设置字段长度8,非空,唯一性索引
    @Column(nullable = false, length = 8, unique = true)
    public String getRightCode() {
        return this.rightCode;
    }

    // 设置金额精度为15,保留小数点后2位
    @Column(precision = 15, scale = 2)
    public BigDecimal getAmount() {
        return amount;
    }

@Convert

javax.persistence.Convert

dodo中特指将@Convert用于枚举类型字段

/**
 * 实体类中使用
 **/
public class BaseTypeDemo extends BaseEntity {
    @DodoField(sortSeq = 13, name = "枚举类型字段")
    private DemoEnum          enumField;

    //......
    // 指定属性值转换器
    @Convert(converter = DemoEnum.Converter.class)
    public DemoEnum getEnumField() {
        return enumField;
    }

    //......
Copyright © Dodo Framework 2019 all right reservedModify At: 2019-12-25 15:03:20