how to update an entity with spring data jpa

后端 未结 5 639
鱼传尺愫
鱼传尺愫 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:26

    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.

    0 讨论(0)
  • 2021-01-31 11:33

    Two ways to make this work

    override compareTo method as

    @Entity(name = "PERSON")
    public class Person implements Comparable<Person>{
     //... all your attributes goes here
     private Integer id;
    
     @Override
    public int compareTo(Person person) {
       return this.getId().compareTo(person.getId());
    }} 
    

    or

    you need to override equals and hashcode methods in your entity class as below

     @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (id == null || obj == null || getClass() != obj.getClass())
            return false;
        Person that = (Person) obj;
        return id.equals(that.id);
    }
    @Override
    public int hashCode() {
        return id == null ? 0 : id.hashCode();
    }
    
    0 讨论(0)
  • 2021-01-31 11:38

    One doesn't have to do any complex operations for this. In your service class you can do something like this.

     @Service
    public class PersonServiceImpl implements PersonService {
    
        @Autowired
        private PersonRepository personRepository;
    
        @Override
        public Person updatePerson(Person oldPerson) throws Exception { 
    
          oldPerson.setId(oldPerson.getId()) ; // pass the associated id for which you want to update and set that id to the same person [ basically setting the same id to the oldPerson ] this way it will not create new entry because here we are not making new ID ] 
    
     //oldPerson.set others [ what is to be updated ]  
    
    
            return personRepository.save(oldPerson); // now save [old person with updated content but same id as it was before ]
        }
    }
    

    This solves the problem of creating new entry to the database and updates the same content with Id associate with that person.

    0 讨论(0)
  • 2021-01-31 11:45

    I think the repository should be

    public interface PersonRepository extends CrudRepository<Person, Integer> {
    

    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.

    0 讨论(0)
  • 2021-01-31 11:48

    You must implement equals() and hashCode() in class Person.

    @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
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (id == null || obj == null || getClass() != obj.getClass())
                return false;
            Person that = (Person) obj;
            return id.equals(that.id);
        }
        @Override
        public int hashCode() {
            return id == null ? 0 : id.hashCode();
        }
    }
    
    0 讨论(0)
提交回复
热议问题