how to update an entity with spring data jpa

后端 未结 5 651
鱼传尺愫
鱼传尺愫 2021-01-31 11:18

I have an entity and a Junit, I want to test that update method is working fine, but when I invoke save method from CrudRepository I get a new entry in my table instead of the u

5条回答
  •  无人共我
    2021-01-31 11:45

    I think the repository should be

    public interface PersonRepository extends CrudRepository {
    

    As your Id is Integer not String, also I assume that your

    personService.createPerson(person); 
    

    Internally use save method of the repo.

    My second advice is the

    @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)}
    

    This mean that the app Context need to be generated again also beans, so please be sure that your configuration in persistence.xml is not having h2bml set to create. Also consider to call flush method in your service.

提交回复
热议问题