1

I want to select specific columns from an entity class and neglect other columns in a method. For this I can not use @JsonIgnore because in another method I want to call all the columns available in that entity class.

@Entity
@Table(name="student")
public class StudentDO {
    @Id
    @Column(name="studentId")
    private int studentId;

    @Column(name="studentName")
    private String studentName;

        @JsonIgnore
    @Column(name="studentPlace")
    private String studentPlace;

    @Column(name="studentstd")
    private String studentstd;

Now I want a method which gives data neglecting @JsonIgnore column and another method to give all the four columns including the column with @JsonIgnore annotation when called.

Could you please help me to solve the issue

Nathan
  • 125
  • 12

2 Answers2

0

you can't do this except you use specific hql or spring data repositories like :

findStudentNameAndStudentstdBysStudentId(int studentId)
Yousef Al Kahky
  • 703
  • 1
  • 8
  • 23
0

This can be achieved with custom entity manager query which returns ResultSet.

Get the EntityManager from SpringBoot context.

@PersistenceContext
private EntityManager entityManager;

Query query = entityManager.createQuery("SELECT s.studentId, s.studentName FROM student s");
List<StudentDO[]> studentRows = query.getResultList();

For each StudentDO, the order of properties corresponds to SELECT parameter order.

For different methods, different custom queries can be written.

Sagar Chilukuri
  • 1,430
  • 2
  • 17
  • 29