title: how to update an entity with spring data jpa
body: <p>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 updated entity.</p>
  
  <p>This is my entity:</p>
  
  <pre><code>@Entity(name = "PERSON")
  public class Person {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "PERSON_ID")
      private Integer id;
      @Column(name = "FIRST_NAME")
      private String firstName;
      @Column(name = "LAST_NAME")
      private String lastName;
  //getters and setters
  }
  </code></pre>
  
  <p>This is my service class:</p>
  
  <pre><code>@Service
  public class PersonServiceImpl implements PersonService {
  
      @Autowired
      private PersonRepository personRepository;
  
      @Override
      public Person updatePerson(Person oldPerson) throws Exception { 
  
          return personRepository.save(oldPerson);
      }
  }
  </code></pre>
  
  <p>This is my repository</p>
  
  <pre><code>public interface PersonRepository extends CrudRepository&lt;Person, String&gt; {
  }
  </code></pre>
  
  <p>This is my test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = { JPAConfigurationTest.class })
  @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
  @Transactional
  public class UpdatePersonServiceIntegrationTest {
          @Autowired
      PersonService personService;
  
          @Before
      public void setUp() throws Exception {
          Person person = new Person(1);
          person.setFirstName("Nicolas");
          person.setLastName("Spessot");
  
          personService.createPerson(person); //This invokes save
      }
  
          @Test
      public void updatingPerson() throws Exception{
          Person person = new Person(1);
          person.setFirstName("Juan");
          person.setLastName("Riquelme");
  
          personService.updatePerson(person);
  
          Person retrieved = personService.retrievePerson(1); //This invokes findOne
  
          assertEquals(1, person.getId());
          assertEquals("Juan", person.getFirstName());
          assertEquals("Riquelme", person.getLastName());
      }
  }
  </code></pre>
  
  <p>Thanks in advance</p>