【问题标题】:Single table inheritance and relationship单表继承和关系
【发布时间】:2018-03-16 10:40:26
【问题描述】:

我正在尝试为这个问题寻找解决方案:

我们在数据库中有一个表 (code_list),其中包含所有类似 enum 的数据。

假设我们有一个Affiliate,它可以有一个AffiliateType 和一个LanguageCode

我们将所有这些都放在code_list 表中,其中id_code_list 字段告诉我们是在谈论AffiliateType 还是Languagecode,我们可以有一个StringInteger 标识符来告诉我们哪个AffiliateType我们在说话。

表中数据示例:

    | id_code_list | val_num | val_string | label  |
    | :----------: |:------: |:---------: | :-----:|
    | TYP_AFF      | 3       | 3          | Other  |
    | TYP_AFF      | 1       | 1          | Divers |
    | COD_LAN      | 1       | 1          | French |

我试图这样映射: 代码列表父级

@Entity
@Table(name = "CODE_LIST")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ID_CODE_LIST")
public abstract class CodeListString {

    @Id
    @Column(name = "VAL_STRING")
    protected String value;

    @Embedded
    protected Label label;
...

会员类型:

@Entity
@DiscriminatorValue("TYP_AFF")
public class AffiliateType extends CodeListString{

    public static final AffiliateType SOCIAL_SECRETARIAT = new AffiliateType("1");
    public static final AffiliateType VARIOUS_SERVICES = new AffiliateType("2");
    public static final AffiliateType OTHERS = new AffiliateType("3");
    public static final AffiliateType SOPA = new AffiliateType("9");

    public AffiliateType() {}

    private AffiliateType(String value) {
        super(value);
    }
}

还有我的Affiliate 实体:

@Entity
@Table(name = "AFF")
public class Affiliate {
    @ManyToOne
    @JoinColumn(name = "TYP_AFF")
    private AffiliateType type;

但我收到此错误:

org.hibernate.MappingException: 外键 (FK7re97tvvbbo2km961gy9b5jw6:aff [typ_aff])) 必须具有与引用的主键相同的列数 (code_list [val_string,id_code_list])

那么,有没有办法让这个工作或者你有其他解决这个问题的方法?

PS:我正在使用 Hibernate 和类似的解决方案

@ManyToOne(targetEntity = AffiliateType.class)
@JoinColumn(name = "TYP_AFF")
@Where(clause = "ID_CODE_LIST='TYP_AFF'")
private AffiliateType type;

没用……

【问题讨论】:

    标签: java hibernate jpa inheritance single-table-inheritance


    【解决方案1】:

    我已经删除了对自定义库的依赖,这很有效。

    我无法在干净的项目中重现此错误。

    无论如何这个解决方案真的很慢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多