【问题标题】:How to design tables for data dictionary when using hibernate?使用hibernate时如何为数据字典设计表?
【发布时间】:2016-04-24 10:55:24
【问题描述】:

我有一个这样的数据字典表:

tbl_data_dictionary

filed

type code code_name

rows

sex 1 boy

sex 2 girl

country 1 American

country 2 China

我有一个数据表:

field

name sex_code country_code

rows

vivic 1 2

如何使用休眠将代码映射到代码名称? one_to_many 映射设置似乎不符合我的需要。将 tbl_data_dictionary 与 tbl_sex 和 tbl_country 分开的唯一方法是什么?但是,如果我有很多类型的字典,而一种类型的行数不多,我觉得这是一种浪费。可以这样休眠地图:sex_code=code and type="sex"

【问题讨论】:

    标签: database hibernate jpa


    【解决方案1】:

    我们的团队将此类通用表用于类似(普通)字典。这是非常不方便的。

    所以,最好使用这种方法:

    • 对于小型词典,请使用枚举。如果您不使用外部 SQL(例如,对于使用 SQL 生成的报告),您可以存储大型枚举的序数。对于小型枚举,可以存储名称。
    • 对于大型字典,使用带有相应实体的单独表。常用字段有@MappedSuperclass 很方便。

    一个例子

    enum Sex {
      MALE, FEMALE, UNKNOWN
    }
    
    @MappedSuperclass
    class Dictionary {
    
        @Id
        private Long code;
    
        @Column
        private String name;
    
    }
    
    @Entity
    class Country extends Dictionary {
    
    }
    
    @Entity
    class User {
    
        @Column
        @Enumerated(EnumType.STRING)
        private Sex sex;
    
        @ManyToOne
        private Country country;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多