I have a method which updates the value of a column and method uses the annotation @Transactional for transaction management.
EXAMPLE CODE TO EXPLAIN THE PROBLEM:
@Transactional
public void methodName(List<SomeClass> listofObjects){
for(someObject sm : listOfObjects){
sm.setName(" lets Assume we are using some dynamic name here "); // assume old name was "stackoverflow" for every record
someClassRepository.saveAndFlush(sm);
}
// Now lets get the list records from DB, by using some native query which retrieves the data
// from a DB view, created by combining 3-4 tables and also have the
// table "SomeClass" ( used above ) and its
// column "Name"
List<ViewObjects> VO = someRepo.getRecords();
for(ViewObjects vo : VO){
sop(vo.getName()); // printing the dynamic names which we updated in above loop
}
}
PROBLEM :
I am using saveAndFlush so that the updated name gets committed to the database. But when in next loop when i get the data from the a view(which is a combination the "SomeClass" and also contain the column "NAME" which we updated) and some other tables. In my for loop the record is printed as "stackoverflow".
I want the updated dynamic name which we inserted.
I also tried to make two different methods , first to update the name, and in second method to get the updated name for DB view, but i got the same output:
@Transactional
public void firstMethod(List<SomeClass> listofObjects){
for(someObject sm : listOfObjects){
sm.setName(" lets Assume we are using some dynamic name here "); // assume old name was "stackoverflow" for every record
someClassRepository.saveAndFlush(sm);
}
}
@Transactional
public void SecondMethod(){
List<ViewObjects> VO = someRepo.getRecords();
for(ViewObjects vo : VO){
sop(vo.getName()); // printing the dynamic names which we updated in above loop
}
}
SOME_CLASS REPOSITORY :
@Repository
public class SomeClassRepository extends JpaRepository<SomeClass, Long> {
}
VIEW_OBJECT REPOSITORY:
@Repository
public class ViewObjectRepository extends JpaRepository<ViewObjects, Long> {
@Query(value="select * from VIEW_OBJECTS", nativeQuery = true)
List<ViewObjects> getRecord();
}
Output needed :
I want to get the updated records with updated NAME column.