首页 > 解决方案 > 如何避免非规范化

问题描述

我有一个实体Category和一个CategoryType存在多对多关系的实体。
在第三个实体Request中,我必须保存this的Categoryand 。 我的伪代码是这样的:CategoryTypeRequest
Request

@Entity
class Request(
id: Long,
title: String,
description: String,
category_id: Long,
category_type_id: Long) {

@JoinColumn(name = "category_id", insertable = false, updatable = false)
@ManyToOne
category: Category

@JoinColumn(name = "category_type_id", insertable = false, updatable = false)
@ManyToOne
category: CategoryType
}

我认为以这种方式设计我的实体是一个坏主意,因为这两个 id (category_idcategory_type_id)之间存在功能依赖(我猜),所以我不尊重第 3 范式。
所以我认为我可以创建一个表示多对多关系的实体,并将这个新实体的 id 保存在表中。
例如:

@Entity
class Request(
id: Long,
title: String,
description: String,
category_to_type_relation_id: Long) {

@JoinColumn(name = "category_to_type_relation_id", insertable = false, updatable = false)
@ManyToOne
category: CategoryToCatTypeRelation
}

@Entity
CategoryToCatTypeRelation(
id: Long,
category_id: Long,
category_type_id: Long)

您认为这可能是一个可行的解决方案吗?还是有更好的方法来避免非规范化?

标签: hibernate

解决方案


推荐阅读