[英]microservices best practice - 3 level data structure
I have a three-level data structure我有一个三级数据结构
level_1 ID, NAME level_1 ID,NAME
level_2 ID, NAME, ID_LEVEL1 level_2 ID,NAME,ID_LEVEL1
level_3 ID, NAME, ID_LEVEL2 level_3 ID,NAME,ID_LEVEL2
The idea is to create a single microservice that manages the CRUDs on the three tables.这个想法是创建一个单一的微服务来管理三个表上的 CRUD。
But what is the best solution?但最好的解决办法是什么?
Do you want to run an API that handles data entry in one go or manage tables with three different API groups?您是要运行一个 API 来处理一个 go 中的数据输入,还是要管理具有三个不同 API 组的表?
I think entering the data in one go leads to the following problems:我认为在一个 go 中输入数据会导致以下问题:
In your case, I recommand you to apply the composit design pattern , generally used to manage hierarchies.在你的情况下,我建议你应用composit design pattern ,通常用于管理层次结构。
And expose a single microservice to CRUD your levels.并将单个微服务公开给您的关卡。
In a single table LEVEL you'll have 3 columns:在单个表LEVEL中,您将有 3 列:
CREATE TABLE `level` (
`id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(500) NULL,
`parentId` int unsigned NULL
);
ALTER TABLE `level`
ADD FOREIGN KEY (`parentId`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
The java class will have: java class 将具有:
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Set;
@Entity
@Table(name = "level")
public class Level
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false)
private int id;
@Column
@Size(max = 500)
private String name;
@ManyToOne()
@JoinColumn(name = "parentId")
private Level parent;
@OneToMany(mappedBy="parent",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Set<Level> children;
public boolean isLeaf() {
return (children == null || children.size() == 0);
}
public boolean isRoot() {
return (parent == null);
}
public int getLevelNumber() {
return getLevelNumber(1);
}
private int getLevelNumber(int currentLevel) {
return parent == null ? currentLevel : parent.getLevelNumber(currentLevel+1) ;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Level getParent()
{
return parent;
}
public void setParent(Level parent)
{
this.parent = parent;
}
public Set<Level> getChildren()
{
return children;
}
public void setChildren(Set<Level> children)
{
this.children = children;
}
}
In my point of you, this is the best and optimized solution.在我看来,这是最好的优化解决方案。
And then I will have a single repository然后我将有一个存储库
public interface LevelRepository extends JpaRepository<Level, Integer>
{
}
If you really need to distinct levels in your database by a discriminator value, then you can add a discriminator values and manage multiple java objects.如果您确实需要通过鉴别器值来区分数据库中的级别,那么您可以添加一个鉴别器值并管理多个 java 对象。 It will give you more visibility in your java project structure, but will really complexify your code and the transaction management.
它将使您在 java 项目结构中有更多的可见性,但确实会使您的代码和事务管理复杂化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.