简体   繁体   English

如何从 Spring 数据 JPA 中的 id 解析不同的实体?

[英]How can I resolve different entities from an id in Spring Data JPA?

Some Background to my application:我的申请的一些背景:
I'm developing a Discord-Bot in Java using Spring Boot, Spring Boot Data JPA and JDA.我正在使用 Spring 引导、Spring 引导数据 Z9CE3D1ZBD8890F108A0C64 和 J38008DD81C2F4D7985ECF6E0CE8AF1D1Z 在 Java 中开发一个 Discord-Bot。
The bot is supposed to have several buttons in Discord with different purposes.该机器人应该在 Discord 中有几个不同用途的按钮。 When a user clicks a button, the bot gets the id (which I gave it beforehand when I created it) of the button in an event-handler.当用户单击按钮时,机器人会在事件处理程序中获取按钮的 id(我在创建它时预先给它)。

Different example scenarios:不同的示例场景:

  1. Button1 with the id 20 resolves to a role-button-entity with Role1, so the Member gets Role1 assigned. id 为 20 的 Button1 解析为具有 Role1 的角色按钮实体,因此成员获得分配的 Role1。
  2. Button2 with the id 32 resolves to a ticket-button-entity with some logic that runs on click. id 为 32 的 Button2 解析为带有一些在点击时运行的逻辑的票据按钮实体。

(The different button entities also have different fields) (不同的按钮实体也有不同的字段)

Question: How can I design something like this?问题:我怎样才能设计出这样的东西?

One option that came to my mind is to have a generic-button-entity, so that JPA's id represents the id from the button in Discord and it stores another id (eg,from a role-button-entity, to run its logic with its fields instead of the ticket-button-entitiy's logic/fields).我想到的一个选择是拥有一个通用按钮实体,以便 JPA 的 id 代表 Discord 中按钮的 id 并存储另一个 id(例如,来自角色按钮实体,以运行其逻辑它的字段而不是票按钮实体的逻辑/字段)。
Does this option make any sense and if so, is it possible to cascade the Long buttonEntityId with eg the role-button-entity?这个选项是否有意义,如果是,是否可以将Long buttonEntityId与例如角色按钮实体级联?

Thanks in advance提前致谢

There are some dessign patterns that would fit in your business case.有一些设计模式适合您的业务案例。 A Factory for instance to decide which button type to create, or a Strategy to decide which behaviour to perform.例如,一个Factory来决定创建哪种按钮类型,或者一个Strategy来决定执行哪种行为。

But either way, every one needs to take a path or another based on some parameter/attribute/property.但无论哪种方式,每个人都需要根据某些参数/属性/属性选择一条或另一条路径。 You are saying that you want to use the ID of the element to do so, that is the same ID that would be used as the PRIMARY KEY in the ddbb.您是说要使用元素的ID来执行此操作,这与将用作 ddbb 中的PRIMARY KEYID相同。 So meaning two different independent tables, but with a shared uniquenes in the ID ...所以意思是两个不同的独立表,但在ID中有一个共享的唯一性......

That, as almost everything in software, can be achieved, but it's a clear code smell, a design smell.就像软件中的几乎所有东西一样,可以实现,但这是一种明显的代码气味,一种设计气味。 Using some id as a discriminator, not for the id value itself, but for the data it represents... Doesn't make sense.使用某个 id 作为鉴别器,不是针对 id 值本身,而是针对它所代表的数据……没有意义。

You can manage to do it, but the solutions you're gonna use, the workarrounds you'll have to perform, and the pitfalls that all that would imply will definitely make your code less readable, maintainable and scalable.您可以设法做到这一点,但是您将要使用的解决方案、您必须执行的解决方法以及所有这些暗示的陷阱肯定会降低您的代码的可读性、可维护性和可扩展性。

A path that would make much more sense is a specific property bound to the buttons that specify the type it is, and use this in the patterns/logic to do/create either one think or another.更有意义的路径是绑定到指定其类型的按钮的特定属性,并在模式/逻辑中使用它来执行/创建一个想法或另一个想法。 Efforts towards this solution will give you much better results.朝着这个解决方案的努力会给你带来更好的结果。

Regarding the database representation of this solution, unless you find a reason why, you don't even need to have a TYPE column, as that can be directly inferred from the TABLE/entity, meaning that it can directly be a constant in the entity.关于这个解决方案的数据库表示,除非你找到原因,你甚至不需要有一个TYPE列,因为可以直接从 TABLE/entity 推断出来,这意味着它可以直接是实体中的常量.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Hibernate & Spring Data JPA 正确注释两个实体之间的这种关系? - How can I properly annotate this relationship between two entities using Hibernate & Spring Data JPA? 如何配置 Spring 引导 JPA Java 实体以自动修剪每个 CHAR 列中的字符串? - How can I configure Spring Boot JPA Java Entities to automatically trim Strings from every CHAR column? 如何在Spring Data JPA或Hibernate中存储实体 - How to store entities in Spring Data JPA or hibernate 如何解决 Spring Data JPA 中的 LazyInitializationException? - How to resolve LazyInitializationException in Spring Data JPA? 两个JPA / Spring数据实体具有相同的结构但表不同 - Two JPA/Spring Data entities with identical structure but different tables 不同方案的两个实体之间的关系 - Spring Boot Data JPA - Relationship between two entities of different schemes - Spring Boot Data JPA 如何在Spring-data-jpa和eclipselink中的不同模块上分布实体? - How to spread Entities over different modules in Spring-data-jpa and eclipselink? 如何使用Spring Data MongoDB或Spring Data Jpa - How can I use Spring Data MongoDB or Spring Data Jpa Select 具有不同于使用 spring jpa 的字段的实体 - Select entities that have a field with occurence different from using spring jpa 使用spring数据jpa的spring boot。 如何从请求主体执行实体的部分更新? - Spring boot with spring data jpa. How can i perform partial update of entity from request body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM