1

For example I have two entities: User and JobOffer. I want display columns from this two entities at one page. I found that this is possible with DTO and my question is whether it is: Is it possible to do it in a different way not using DTO?

User.java

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_user")
private Integer id;

@Column(name = "name",nullable = false)
private String name;

@Column(name = "last_name",nullable = false)
private String lastName;

@Column(name = "email",nullable = false, unique = true)
private String email;

@Column(name = "password",nullable = false)
private String password;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<JobOffer> jobOffers;

JobOffer.java

@Entity
@Table(name = "job_offer")
public class JobOffer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idjob_offer")
private Integer id;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "location", nullable = false)
private String location;

@Column(name = "description" , nullable = false)
private String description;

@Column(name = "contact", nullable = false)
private String contact;

@Column(name = "category", nullable = false)
private String category;

@Column(name = "company_name", nullable = false)
private String companyName;

@Column(name = "contract_type", nullable = false)
private String contractType;

@Column(name = "working_time", nullable = false)
private String workingTime;

@Column(name = "monthly_pay", nullable = false)
private String monthlyPay;

@Column(name = "position_level", nullable = false)
private String positionLevel;

@ManyToOne
@JoinColumn(name = "id_user")
private User user;

JobOfferDTO.java

public class JobOfferDTO {

private String title;
private String location;
private String category;
private String email;
//getters setters ...

JobOfferRepository.java

@Repository
public interface JobOfferRepository extends JpaRepository<JobOffer, Integer> {

@Query("Select new com.biuropracy.demo.DTO.JobOfferDTO(j.title, j.location, j.category, u.email) 
from JobOffer j join  j.user u")
public List<JobOfferDTO> getJobOfferDTO();
}

Everything works fine but is there any other maybe better or simple way to display this columns at one page?

Piotror
  • 85
  • 1
  • 12

1 Answers1

1

First of all you need to consider what tasks are you doing in your repository.

  • You are retrieving the info about two entities in one query.
  • You are matching Jobs to Users by joining.
  • You are converting the results you ask in the select clause to a "JobOfferDTO".

Now, let's see if there is another way to complete these tasks without using a DTO.

  • First of all, you have to expose a DTOs from your API and not Entities, here you can find some answers on why you want that. So in other words, at least in you Controller level, you will end up with a DTO.
  • Let's see now if you can avoid a DTO in you DAO level. If you keep your entities as they are now, then you would need to make two separate queries and then match the job offers to the relative users programmatically by yourself in the Service level. Which is something that the join already does for you in the query.

    The only other alternative would have been to refactor your entities and create one entity that would map both the users and the job offers tables. Take a look here for an example. But then again you would need to convert that single entity to its relative DTO before exposing.

  • Finally about the third task, the conversion of an entity to a DTO, if you decided to retrieve entities from your query, you would need to create methods to convert to the relative DTOs. A mapper for converting User to UserDTO and Job to JobDTO, or if you create an entity of users with the secondary table of job offers you would have a mapper for userJobs to userJobsDTO for example. So having your query take care of the mapping too, is also profit for you.

I hope this answers your question.

Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40