1

I'm using Spring data jpa and spring mvc And i noticed that my object contact is updated in database automatically.

Here's My Code :

@Transactional
@Controller
public class ContactController {
    @RequestMapping(value="/update_contact ") 
    public @ResponseBody
    String update_contact (...) {
        ...
        Contact contact = contactrespository.findOne(idcontact);

        contact.setName(...);
        ...

    }
}

And without executing contactrespository.save(idcontact); My contact has been changed when i checked the database !
Can you explain me Why ?

1 Answer 1

3

There are many states of an Object :

  • Persistent: a persistent instance has a representation in the database and an identifier value and it is associated with a Hibernate Session.
  • Detached : a detached instance is an object that has been persistent, but its Session has been closed.
  • transient: an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session.

In this context The changes are committed to contact because it is a persistent object that has been modified within a transaction because your controller is annotated with @Transactional so it's associated with a Hibernate Session.

It is not a good practice to annotate a Controller with Transactional annotation, it is better used on the service Layer where we invoke the repository not in the controller layer

    @Controller
    public class MyController{

        @Autowired
        private MyService service;

        @RequestMapping ....
        public Contact findContact(String name, ....){
        Contact contact =  service.get(...);
         // other logic 
         }
     }




    @Service
    public class MyService{

        @Autowired
        private MyRepository repository;

        @Transactional(propagation=Propagation.SUPPORTS)
        public Contact get(long id){
         // better throw a notFuondException in here 
        return repository.findOne(id);
        }
    //same for other method like create and update with @Transactional REQUIRED NEW  or      REQUIRED propagation
   }
Sign up to request clarification or add additional context in comments.

15 Comments

yes i know they say it's better to put Transactional on service layer but it's not a obligation. So even i'll make a service layer like you said i will have the same behavior -> the object will get updated automatically.
i said it is not a good practice,you risk dirty reads and a mass overflow connections to database, means every time a client loads the page a change is made to the database !! its MVC, it supposed to be light weighted and independent from the persistence layer,it suppose to be a Client to the persistence layer which means to the services.
thanks for the information So do you mean even if i don't make something like contactrespository.findOne(idcontact) in my Controller the connection will be opened to the database ?
But your answer didn't explain my issue ... your answer is just a advise because even if I put a service layer my issue will still remain.
findOne will return a Persistent instance of Contact , means every change made to this instance will be applied to the database.no change will be maid if the instance is Detached
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.