0

Given the following architecture and frameworks: Spring Boot Application with Spring Data JPA (Hibernate is used as OR mapper); layered architecture as followed.

  • REST layer
  • Service layer
  • Persistence layer (JPA)

DTOs are used to transfer the data between frontend and backend. Mapping for DTOs -> entity objects, entity objects -> DTOs do already exist.

My questions are:

  • Should DTOs contain only the id as a reference to another object (DTO) or the other DTO object itself as reference type?
  • Should entities contain only the id as a reference to another object (entity) or the enitity object itself as a reference type?

To be more precise, eg given an fictive application where orders and items exist. An order consists of multiple items. How should the corresponding DTO (1) and entity object (2) look like:

(1)(a)

public class OrderDTO {
   private Long id;
   private List<Long> items; // id of item entities
   // further stuff omitted
}

or

(1)(b)

public class OrderDTO {
   private Long id;
   private List<ItemDTO> items;
   // further stuff
}

whereas ItemDTO is

public class ItemDTO {
   private Long id;
   private String name;
   // further stuff
}

(2)(a)

@Entity 
public class Order {
   @Id
   // value generation strategy would be set here
   private Long id;

   // some JPA annotaions to reference the id column of items
   private List<Long> items;
   // further stuff omitted
}

or

(2)(b)

@Entity
public class Order {
   @Id
   // value generation strategy would be set here
   private Long id;

   @OneToMany
   // further annotations
   private List<Item> items;
   // further stuff omitted
}

whereas Item is

@Entity
public class Item {
   @Id
   // value generation strategy would be set here
   private Long id;
   private String name;
   // further stuff
}

I would be glad if you could share you best practices/opinion. Thanks.

  • This is somewhat opinion-based and depends on the situation (what is needed for your application), but I would say: object references and not IDs. Especially in the JPA entities; use @ManyToOne / @OneToMany etc. to link entities (and be careful about cascading etc.). There is not one best solution for any situation. – Jesper May 12 at 21:51
  • Hi Jesper, thanks. Do you think that a "mixed approach" would be reasonable in general? I was thinking of using IDs in the DTOs (also in order to reduce the amount of data transfered) and object references in the entities, since then i can use @OneToMany etc as stated by you. – keezar May 12 at 22:15
  • You should design your DTOs for what data exactly you want to send to the client. You don't want to be sending the whole database for a simple request, so I would use IDs in the DTOs if that gives you what you need for the client. – Jesper May 13 at 11:38
1

These all eventually make it into a database having IDs anyway, so my vote is for IDs.

Object references can have the peculiar property of pointing to two different objects that are functionally equal. This is why you compare strings in Java using .equals(), which compares the characters in one string against the characters in the other; and not == which compares the two references and always returns false, even if the two strings have the same sequence of characters.

Object references are also not persistent; they only exist in memory. You can store an ID in a database, and successfully retrieve it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.