简体   繁体   English

仅发布单向多对一关系的ID

[英]Posting just the id of unidirectional many to one relationship

I am submitting a POST request (JSON) to create a new instance of a Person and each Person has to have a Status associated with them. 我正在提交POST请求(JSON),以创建一个Person的新实例,并且每个Person都必须具有与之关联的Status。 There is a prepopulated Status table that has a limited number of statuses. 有一个预填充的状态表,其状态数量有限。 Is just submitting the entity id of the status the best approach here? 这里只是提交状态的实体ID是最好的方法吗?

Currently, I submit just the id of the Status entity in my request, but this means loading a Status from the DB using the id and adding it to the transient Person before it's persisted beforehand to satisfy the JPA/DB constraints, but this is inefficient. 当前,我仅在请求中提交Status实体的ID,但这意味着使用该ID从数据库加载状态,然后将其添加到临时Person之前要持久保存以满足JPA / DB约束,但这效率低下。

Request body (truncated for brevity) unmarshalled to PersonDTO: 请求正文(为简洁起见已被截短)未编组到PersonDTO:

{
    "name" : "Dave",
    "statusId" : "1",
    ::
}

Status entity 身份实体

@Entity
public class Status {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Column
    private String status;
}

Person entity 人实体

@Entity
public class Person {

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE)
   private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "status_id")
    private Status status;
    ::

Is the best approach actually submitting the status object in the JSON request so I don't have to perform any sort of lookup on the status id? 最好的方法实际上是在JSON请求中提交状态对象,这样我就不必对状态ID进行任何形式的查找吗?

{
    "name" : "Dave",
    "status" : {
      "id" : 1
      "status" : "AVAILABLE"
    },
    ::
}

I appreciate that I can preload the statuses at startup into a singleton so they are accessible or cache a service class method that returns a status using the id, but I am wondering what the best approach is here because if I was using native SQL I could just insert the Person into the database with a status id and not perform any lookup of the status beforehand. 我很欣赏我可以将启动时的状态预加载到一个单例中,以便可以访问它们或缓存使用ID返回状态的服务类方法,但是我想知道最好的方法是什么,因为如果使用本地SQL,我可以只需将带有状态ID的Person插入数据库中,而无需事先对状态进行任何查找。

1) Once you start using Native SQL, you will start using it in more and more places. 1)一旦开始使用Native SQL,您将在越来越多的地方开始使用它。 Eventually, you will lose the database provider independence that JPA gives you. 最终,您将失去JPA给您的数据库提供程序独立性。 I am not saying it is 100% bad, I am just saying.. 我并不是说这是100%不好,我只是说..

2) You can apply the second-level-cache for the Status as it seems to be a sort of dictionary table: 2)您可以为Status应用二级缓存,因为它似乎是一种字典表:

@Entity
@Cacheable

3) If you are not expecting to make any changes to the status object itself, you should load it not using the findById , which goes to the database, but use the getOne instead which only creates a reference and should be sufficient in your case. 3)如果您不希望对状态对象本身进行任何更改,则不应该使用findById来加载状态对象,后者会进入数据库,而应使用getOne代替,后者只会创建一个引用,在您的情况下就足够了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM