Have a "full Entity" class:
@Entity(name = "vacancy_dec_to_words") public class VacancyDescriptionToWords { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @JoinColumn(name = "vacancy_description_id") @ManyToOne(cascade = CascadeType.ALL) private VacancyDescription vacancyDescription; @JoinColumn(name = "words_id") @ManyToOne private Words words; @Column(name = "qty") private int qty; @Column(name = "create_date") private Date date; //...getters and setters In some methods I need use only 2 column from this database: word_id and qty
I try the follow way:
Projections https://docs.spring.io/spring-data/jpa/docs/2.1.2.RELEASE/reference/html/#projections
public interface QtyWords { Long getWords(); Integer getQty(); } JpaReposytory:
*Query, that I use tested and it workable, I use him in JpaRepository:
@Repository public interface SmallVDTWRepository extends JpaRepository<VacancyDescriptionToWords, Long> { @Query(nativeQuery = true, value = "SELECT sum(qty), words_id FROM vacancy_desc_to_words WHERE vacancy_description_id IN (" + "SELECT id FROM vacancy_description WHERE vacancy_id IN (" + "SELECT id FROM vacancy WHERE explorer_id = :exp))" + "GROUP BY words_id") List<QtyWords> getDistinctWordsByExplorer(@Param("exp") long exp); } But I get some interesting result when I get list of entities:
List<QtyWords> list = vdtwService.getByExplorerId(72); I am not get any exceptions, but I have the list with are unknowns objects. This objects contains my data, which I need(qty and words_id), but I cannot get them from him.
Can I use this method (Projection) to implement this task and, in general, how to correctly implement the 'Light Entity' in this case?
