数据模型

 

public class OcOrg{
    private String name;
    private String code;
    private List<OcUser> users; //关联用户
    private List<OcOrg> parents; //上级机构
    private List<OcOrg> children; //下级机构
}

 

 

public class OcUser{
    private OcOrg ownOrg;
    private String address;
    private List<OcRole> roles; //关联用户角色
}

 

 

public class OcRole{
    private String key;
}

 

 

getAssociationsForNotCascade(定义哪些class中的字段不作级联更新)

 

public List<Pair<Class, String>> getAssociationsForNotCascade(){
 
    List<Pair<Class, String>> result = new ArrayList<>();
 
    //OcUser实体新增、更新、删除时,对关联的OcOrg实体不作级联处理
    result.add(new ImmutablePair<>(OcUser.class"ownOrg"));
 
    return result;
}

 

 

getClassAndTableNameAndIndexNameAndUniqueNameListPairs(设置Entity映射到数据库中的表名、及需增加索引的列和唯一键)

 

public Map<Class, Triple<String, List<String>, List<String>>> getClassAndTableNameAndIndexNameAndUniqueNameListPairs() {
 
    Map<Class, Triple<String, List<String>,List<String>>> result = new HashMap<>();
 
    //ImmutableTriple中的第一个参数表示映射到数据库中的表名(OcOrg默认的表名可能是OcOrg,我们可以自定义表名为Org)
    //第二个参数是表上需增加的索引。List中的每个元素代表一个索引,组合索引用逗号分隔。(范例中 name+code 为一个组合索引)
    //第三个参数是表上需增加的唯一键。List中的每个元素代表一个唯一键,组合唯一键用逗号分隔。(范例中 name是一个唯一键、code是一个唯一键)
    result.put(OcOrg.class,new ImmutableTriple("Org", Arrays.asList("name,code"),Arrays.asList("name","code")));
 
    return result;
 
}

 

 

getOneToManyAndManyToOnePairList(配置OneToMany和ManyToOne的映射关系)

 

public List<Pair<Pair<Class, String>, Pair<Class, String>>> getOneToManyAndManyToOnePairList() {
 
    List<Pair<Pair<Class, String>, Pair<Class, String>>> result = new ArrayList<>();
 
    //如不配置OneToMany和ManyToOne的映射关系,则Hibernate做ORMapping映射时会生成一个连接OcOrg和OcUser的中间表
    result.add(new ImmutablePair<>(new ImmutablePair<>(OcOrg.class"users"), new ImmutablePair<>(OcUser.class"ownOrg")));
 
    return result;
 
}

 

 

getManyToManyFieldsForCollectionType(配置ManyToMany的映射关系)

 

public List<Triple<Class, String, String>> getManyToManyFieldsForCollectionType() {
 
    List<Triple<Class, String, String>>  result = new ArrayList<>();
 
    //用户和角色为多对多的关系,如不指定ManyToMany, 默认是OneToMany的映射关系。 roles为字段名
    result.add(new ImmutableTriple<>(OcUser.class"roles"""));
 
    //机构有多上级、多下级。针对这种在Entity中对自身Entity的父子引用,按如下方式进行配置。 parent、children为字段名
    result.add(new ImmutableTriple<>(OcOrg.class"parents"""));
    result.add(new ImmutableTriple<>(OcOrg.class"children""parents"));
 
    return result;
 
}

 

 

getCustomFields(设定Entity中的字段在数据库中的字段名、字段类型)

 

public List<Triple<Class, String, ColumnImpl>> getCustomFields() {
    List<Triple<Class, String, ColumnImpl>> result = new ArrayList<>();
 
    //OcRole中的字段key,在某些数据库中是关键字,故需指定一个特殊的名称(oc_key)
    result.add(new ImmutableTriple<>(OcRole.class"key"new ColumnImpl("oc_key"30)));
     
    //OcUser中的字段address,默认数据库中的类型及长度是varchar(255),我们如果想自定义类型或长度,需特殊指定。如下范例中将该字段定义为一个text类型。(针对大对象类型,此处需根据实际数据库类型来进行设定)
    result.add(new ImmutableTriple<>(OcUser.class"address"new ColumnImpl("""text")));
    return result;
}

 

 

getJoinTableNames(指定中间表的表名)

 

public List<Triple<Class, String, String>> getJoinTableNames() {
    List<Triple<Class, String, String>> result = new ArrayList<>();
 
    //针对集合类型字段生成的中间表,默认生成的中间表名为:当前Entity.name + "_" + 字段名 + "_" + 关联Entity.name, 如OcUser_roles_OcRole。映射到数据库中时,某些数据库对表名的长度有限制,比如Oracle11G,表名长度最大为32位。此时就需要指定中间表的表名
    result.add(new ImmutableTriple<>(OcUser.class"roles""UserRoles"));
     
    return result;
}