hibernate中的多个连接关联

发布于 2024-11-18 23:34:07 字数 1289 浏览 3 评论 0原文

我的问题与数据库设计以及如何在 Hibernate 中对该设计进行建模有关。我有两个具有以下主键的表:

BLOCK (BLOCK_ID)
BLOCK_SHP (BLOCK_ID, SHAPE_VERSION)

BLOCK 到 BLOCK_SHP 是一对多关系,因为单个块可以具有与其关联的许多不同版本的形状。到目前为止,一切都很好。

第二个关联是我还希望能够获取块的当前形状。为此,我向 BLOCK 表添加了另一个属性:

CUR_SHAPE_VERSION

BLOCK_ID 和 CUR_SHAPE_VERSION 现在形成 BLOCK_SHP 表中 BLOCK_ID、SHAPE_VERSION 的外键。每个块可以有 0 或 1 个电流形状。

在 Hibernate 中,我通过以下方式设置了第二个关联:

@OneToOne( cascade = CascadeType.ALL, optional = true )
@NotFound( action = NotFoundAction.IGNORE )
@JoinColumns( {
  @JoinColumn( name = "BLOCK_ID", referencedColumnName = "BLOCK_ID", insertable = false, updatable = false ),
  @JoinColumn( name = "CUR_SHAPE_VERSION", referencedColumnName = "SHAPE_VERSION", insertable = false, updatable = false ) } )
public BlockShape getCurrentShape() {
    return currentShape;
}

需要 @NotFound 注释,因为 Hibernate 在处理可为空的一对一关联时遇到困难。如果它没有找到关联,它会忽略它而不是抛出错误。

但这对我来说不是很令人满意,因为这意味着 Hibernate 并没有真正意识到实体之间的正确关系。例如,如果我查询 currentShape is not null,Hibernate 不知道如何正确执行此查询 - 它正在查询 block_id is not null 或 cur_shape_version is not null

所以我想我有几个问题。首先,是否有更好的方法来对数据库中的第二个关联进行建模?其次,Hibernate中有没有更好的方法来设置注释,以便更好地理解关系并能够正确查询形状表?

感谢您的任何帮助。

My question is related to database design and also how to model that design in Hibernate. I have two tables with the following primary keys:

BLOCK (BLOCK_ID)
BLOCK_SHP (BLOCK_ID, SHAPE_VERSION)

BLOCK to BLOCK_SHP is a one-to-many relationship as a single block can have many different versioned shapes associated with it. So far so good.

The second association is that I also want to be able to get the current shape for the Block. To do this, I have added another attribute to the BLOCK table:

CUR_SHAPE_VERSION

BLOCK_ID and CUR_SHAPE_VERSION now form a foreign key to the BLOCK_SHP table on BLOCK_ID, SHAPE_VERSION. Each block may have 0 or 1 current shapes.

In Hibernate, I have set this second association up in the following way:

@OneToOne( cascade = CascadeType.ALL, optional = true )
@NotFound( action = NotFoundAction.IGNORE )
@JoinColumns( {
  @JoinColumn( name = "BLOCK_ID", referencedColumnName = "BLOCK_ID", insertable = false, updatable = false ),
  @JoinColumn( name = "CUR_SHAPE_VERSION", referencedColumnName = "SHAPE_VERSION", insertable = false, updatable = false ) } )
public BlockShape getCurrentShape() {
    return currentShape;
}

The @NotFound annotation was required because Hibernate was having trouble dealing with nullable one-to-one associations. If it doesn't find the association it ignores it instead of throwing an error.

This isn't very satisfying to me though, because it means that Hibernate isn't really aware of the proper relationship between the entities. For example, if I query for currentShape is not null, Hibernate does not know how to perform this query properly - it is querying on block_id is not null or cur_shape_version is not null.

So I guess I have a few questions. First, is there a better way to model this second association in the database? Second, is there a better way in Hibernate to set up the annotations for it to better understand the relationship and be able to properly query on the shape table?

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2024-11-25 23:34:07

最简单的方法是使用 Shape 实体的代理主键。这些表看起来像这样:

BLOCK (BLOCK_ID primary key, CURRENT_SHAPE_ID foreign key references SHAPE.SHAPE_ID)
SHAPE (SHAPE_ID primary key, SHAPE_VERSION, BLOCK_ID foreign key references BLOCK.BLOCK_ID)

Hibernate 不鼓励使用复合键,这是有充分理由的(您遇到的问题只是其中之一)。

The easiest way is to use a surrogate primary key for the Shape entity. The tables would look like this:

BLOCK (BLOCK_ID primary key, CURRENT_SHAPE_ID foreign key references SHAPE.SHAPE_ID)
SHAPE (SHAPE_ID primary key, SHAPE_VERSION, BLOCK_ID foreign key references BLOCK.BLOCK_ID)

The use of composite keys is discouraged by Hibernate, for good reasons (and the problem you're having is just one of them).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文