Spring boot : DB entity not getting updated - using Transitional @Annotation

Viewed 607

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.

2 Answers

In your example, SomeClass and ViewObjects are different JPA Entities, which are mapped to different database tables.

For example, if ViewObjects is mapped to "VIEW_OBJECTS" table and SomeClass is mapped to "SOME_CLASS" table, then of course updating "SOME_CLASS" table has nothing to do with "VIEW_OBJECTS" table.

But if "VIEW_OBJECTS" is not a table, but a view, then please, provide more detailed information on your database schema ("SOME_CLASS" and "VIEW_OBJECTS"). Also, please, provide the code for SomeClass and ViewObjects entities (with all JPA annotations). Would also be nice to see your database properties file, so we could see your jdbc properties, which database you use, which jpa provider you use and all other properties, which might be related to your transaction configuration

Otherwise, the following changes should fix the issue:

@Repository
public class SomeClassRepository extends JpaRepository<SomeClass, Long> {
@Query(value="select * from SOME_CLASS", nativeQuery = true)
    List<SomeClass> getRecord();
}

The second method should use someClassRepository instead:

@Transactional
public void SecondMethod(){
List<SomeClass> VO = someClassRepository.getRecords();
       for(SomeClass vo : VO){
        sop(vo.getName()); // printing the dynamic names which we updated in above loop
       }
}

You need to look at the database directly, to understand what is the issue.

  1. Open your database client
  2. Is the name of the correspondent SOME_CLASS table record updated?
  3. If "no", then there is a problem with your jdbc or jpa configuration. Please, provide more information on your jdbc configuration, jpa configuration, which database you use. Also, take a look at the logs: maybe no sql update statements are triggered at all?
  4. If "yes", then go to 5
  5. Is the name of the correspondent VIEW_OBJECTS view record updated?
  6. If "no", then the problem is in your VIEW_OBJECTS view. Please, provide the SQL Definition of the VIEW_OBJECTS view, so we can understand better what is the issue.
  7. If "yes", then the problem is in ViewObjects entity. Please, provide the code for you "ViewObjects" class, so we can understand better what is the issue
Related