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?