java - Spring JPA - cannot update in while loop, insert works -


i using spring data/jpa perform database operations. have while loop runs , inserts data runs, need update operation happen @ end of each run of while loop. here have in simple example. structure using.

class doing operations:

@component public class myclassimpl implements myclass {     @autowired     myotherclass myotherclass;      @override     public void run() {         while (expression) {             // data , entity object              myotherclass.insertmethod(entity);             myotherclass.updatemethod(entityid);         }     }   } 

my other class:

@component public class myotherclassimpl implements myotherclass {      @override     jpaclass jpaclass;      @override     @transactional(propagation = propagation.requires_new)     public void insertmethod(entityobject entity) {          jpaclass.save(entity);     }      @override     @transactional(propagation = propagation.requires_new)     public void updatemethod(string entityid) {          entityobject entity = jpaclass.findbyid(entityid);          //change on entity          jpaclass.save(entity);     } } 

entity object:

public interface jpaclass extends jparepository<entityobject, long> {       entityobject findbyid(string entityid); } 

the problem having insert works fine, within while loop cannot updates work have them. have tried moving logic around , putting findbyid logic in different method cannot working. trying update 1 row in table handles 1 value need reference in next run of while loop.

so goes:

  1. get value
  2. operate using value
  3. update value
  4. repeat

i set database config using spring @configuration on class works fine transactions, reference set this:

@configuration @enabletransactionmanagement @propertysource(value = { "classpath:/${app.execution.environment}/application.properties" }) @enablejparepositories(basepackages = "com.example", entitymanagerfactoryref = "mysqlentitymanager", transactionmanagerref = "mysqltransactionmanager") public class mysqlhibernateconfig {  // needed beans here } 

just confirm well, ran logic without while loop , data update expected, problem somewhere in database transaction, stuck on how resolve it.


Comments