【问题标题】:How to map multiple @OneToOne associations from same table如何从同一个表映射多个@OneToOne 关联
【发布时间】:2018-11-25 14:29:39
【问题描述】:

我目前有一个简单的应用程序,其中我的 Container 类的属性映射到数据库列。

今天...

@Entity
class Container {
    @Column
    private BigDecimal propertyA;

    @Column
    private BigDecimal propertyB;
}

我正在尝试扩展我的设计,以允许将每个逻辑属性的多个值存储在一起。我现在想为每个属性存储 5 个值来表示它们的不确定性(有些可能为空)

在数据库模式级别,可以这样表示。我不相信每个不确定性都需要它自己的 id,因为 (container_id, data_field) 的组合是唯一的。

CREATE TABLE container (
    id INT PRIMARY KEY,
    ...
);

CREATE TABLE uncertainty (
    container_id bigint not null,
    data_field varchar(100) not null,
    maximum decimal null,
    upside decimal null,
    reference decimal null,
    downside decimal null,
    minimum decimal null,
    primary key (container_id, data_field),
    constraint fk_container_id foreign key (container_id) references container (id),
);

对于Container 中需要此功能的每个属性,表uncertainty 中将有一行。

在上面的示例中,uncertainty 中有两行,data_field 的值是 propertyApropertyB

这样的东西会很方便..

@Entity
class Container {

    // How do I map multiple @OneToOne relationships to the uncertainty table based on e.g. the value of data_field = 'propertyA' or data_field = 'propertyB'

    private Uncertainty propertyA;
    private Uncertainty propertyB;
}

@Entity
class Uncertainty {

    // Do I need fields here to represent the composite primary key (containerId, data_field)?

    @Column
    private BigDecimal maximum;

    @Column
    private BigDecimal upside;

    @Column
    private BigDecimal reference;

    @Column
    private BigDecimal downside;

    @Column
    private BigDecimal minimum;
}

映射这种关系与 JPA/Hibernate 的最佳方式是什么?

非常感谢。

【问题讨论】:

    标签: java database hibernate jpa


    【解决方案1】:

    您所描述的是 OneToMany 关系,其中每个 Container 都有一个不确定性集合。

    @Entity
    class Container {
        @JoinColumn(name = "container_id")
        private Collection<Uncertainty> properties;
        ...
    }
    
    @Entity
    class Uncertainty {
        ...
    }
    

    在 Uncertainty 中不需要额外的东西来映射它。

    【讨论】:

    • 谢谢。我希望避免将它们捆绑在一起,因为我必须继续过滤 Collection 以查找 propertyA 或 propertyB 等的值。我真的很想将它们映射到 Container 对象中的单个变量。这不可能吗?
    • 如果您在编译期间知道 Uncertainty 对象的确切数量,这是可能的。您将必须拥有这个确切数量的引用,就像您自己写的那样:private Uncertainty propertyA;私有不确定性属性 B 等等。此外,您的 Uncertainty 类需要一个 id(这样注释).. 并且所有对它的引用都是 OneToOne
    【解决方案2】:

    你不需要复合键,你需要一个@Id

    @Entity
    class Uncertainty {
    
    // Do I need fields here to represent the composite primary key (containerId, data_field)?
    
    @Id
    private int id;
    
    @Column
    private BigDecimal maximum;
    
    @Column
    private BigDecimal upside;
    
    @Column
    private BigDecimal reference;
    
    @Column
    private BigDecimal downside;
    
    @Column
    private BigDecimal minimum;
    

    }

    并且引用(propertyA、propertyB 等)应该是:

    @Entity
    class Container {
    
    // How do I map multiple @OneToOne relationships to the uncertainty table based on e.g. the value of data_field = 'propertyA' or data_field = 'propertyB'
    
        @OneToOne
        private Uncertainty propertyA;
        @OneToOne
        private Uncertainty propertyB;
    }
    

    当然,您需要知道要引用的对象的确切数量

    【讨论】:

    • 谢谢。我知道编译时的数字。会有这样一组固定的属性。这肯定更像我正在寻找的最终结果。但是,我仍然对@Id 感到困惑。更具体地说,我不需要在某处引用 data_field 吗?
    • 您为每个不确定性对象分配一个 ID,它可以自动生成:@GeneratedValue(strategy=GenerationType.IDENTITY),然后,在您分配的每个引用对象上,例如:propertyA = new Uncertainty(5 ) - 将指向 ID 为 5 的不确定性对象。当然,您必须管理这些对象并以某种方式知道它们的 ID。您还可以使用 JPA 检索它们并分配它们
    • 我希望利用 (container_id, data_field) 已经是一个键这一事实,并以某种方式在映射中描述它。这似乎是不可能的,或者我从错误的角度处理事情。
    猜你喜欢
    • 2021-10-30
    • 2010-12-12
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多