Announcement Announcement Module
Collapse
No announcement yet.
Object states with spring data repository. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Object states with spring data repository.

    The question may sound stupid, sorry in advance if it is
    I have a project that uses spring - data, jpa and hibernate for jpa impl.

    When I do select an entity does it mean entity is associated with a persistence context? or it will be already detached? I do not know how spring-data uses EntityManager object for queries under the hood, when it is begin closed , when it is begin flushed?

    When my entity object is going to be detached? Right away after selection?
    When my entity object is going to be attached? Right away after merge?
    When my entity object is going to be persisted? Right away after persist? When I do persist I should be able to still update my objects before flushing repo.

    I saw that repository exposes flush method, and repository itself is singleton bean injected into class as a spring bean. So for concurrency usage how does flush work? How does life cycle work while concurrent threads are using the same repo and calling flush for different purposes?


    If some doc out there, I would love to read. i will check the doc again.

    thanks for response.

  • #2
    The usage of spring-data-jpa doesn't change how JPA works. So basically your questions are answered by reading up on JPA.

    However it also depends a little on your configuration, how are your transactions configured and do you use an OpenEntityManagerInViewFilter. However that is all documented in the Spring Reference guide.

    Comment


    • #3
      Hi thanks for the answer.

      For example this scenario, When/How I will ever have this kind of scenario in the same method :

      em1.getTransaction.begin(); // new connection to data store is obtained and tx started
      foo = em1.get(Foo.class, 1);
      em1.getTransaction().commit(); // End tx, flush

      ......after few lines of code...........
      call setters and getts on foo

      em2.getTransaction.begin(); // new connection to data store is obtained and tx started
      em2.pesist(foo);
      em2.getTransaction().commit(); // End tx, flush and check version, disconnect


      Because I do not know jpa repository is using the em object which I was using for pulling foo or it is already dead and using new em. spring - data is doing those things behind the scene and I do not know how em instances are being used by spring-data. Spec does not say anything about this kind of things.

      Comment

      Working...
      X