Jeya Nandhana
Jeya Nandhana

Reputation: 358

Update the some value in object and set to list in spring boot

I have problem to update the value and set the list.

StudentEntity.class

public class StudentEntity{
  private String name;
  private int rollNo;
  private String address;
}

Student.class

public class Student{
   private String name;
   private int rollNo;
   private String address;
}

Now I will change the address from old data. First,I am getting the data from database.

Map the entity to model class using ObjectMapper.

Student student=new Student();
studentEntity=studentRepository.findOne(id);    
student=objectMapper.convertValue(studentEntity, Student.class);
student.setAddress("Bangalore");
List<Student> listOfStudent=new ArrayList();
listOfStudent.add(student);  

Finally I returned the list of Student. But value is not updated.It showed the old one.

Upvotes: 0

Views: 2657

Answers (2)

user9065831
user9065831

Reputation:

The issues is in objectMapper.convertValue you have to pass Student.class as Second parameter.

student = objectMapper.convertValue(studentEntity, Student.class);

Upvotes: 1

Kapil
Kapil

Reputation: 917

You are converting studentEntity to StudentEntity again. instead it should be like following.

 student = objectMapper.convertValue(studentEntity, Student.class); 

Upvotes: 2

Related Questions