Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update an entity with spring data jpa

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.

This is my entity:

@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
}

This is my service class:

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    @Override
    public Person updatePerson(Person oldPerson) throws Exception { 

        return personRepository.save(oldPerson);
    }
}

This is my repository

public interface PersonRepository extends CrudRepository<Person, String> {
}

This is my test:

@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());
    }
}

Thanks in advance

like image 315
nspessot Avatar asked Feb 26 '14 03:02

nspessot


People also ask

Which method is used to update entity in JPA?

We use the EntityManager. merge() method to update an entity. This method takes the entity to be saved as the parameter and return the merged entity back as the result.

Which method is used for update in JPA Repository?

You can modify or update the entities by defining the repository method using @Query and @Modifying annotations. All the update/delete operations are based on the transaction scope.


1 Answers

The problem is in your updatePerson method in your service class. Specifically:

return personRepository.save(oldPerson);

All you are currently doing is saving a Person. That's why it is creating a second entry.
What you should do is find the oldPerson first,

Person p = personRepository.findOne(oldPerson.getId())

then update its attributes, and then save it like you did before. Hope that helps.

like image 126
chinesewhiteboi Avatar answered Oct 04 '22 20:10

chinesewhiteboi