It is good that the question as a whole speaks about everything. Using JPARepository how to update an object?
JPARepository has only a save method, which does not tell me if it really creates or updates. For example, I insert a simple object into the User database, which has three fields: first and last name and age:
@Entity public class User { private String firstname; private String lastname; //Setters and getters for age omitted, but they are the same as with firstname and lastname. private int age; @Column public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } @Column public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } private long userId; @Id @GeneratedValue(strategy=GenerationType.AUTO) public long getUserId(){ return this.userId; } public void setUserId(long userId){ this.userId = userId; } }
Then I just โsaveโ, which at the moment is an insert:
User user1 = new User(); user1.setFirstname("john"); user1.setLastname("dew"); user1.setAge(16); userService.saveUser(user1);
Good, all is well. Now I want to update this user, say, change his age. Well, I could use Query for QueryDSL or NamedQuery, regardless. But, given that I just want to use spring-data-jpa and JPARepository, how do I say that instead of pasting I want to do an update?
In particular, how can I tell spring -data-jpa that users with the same username and first name are actually EQUAL and that the object is supposed to be updated. The redefinition of peers did not work.
Thank!
java spring-data-jpa jpa
Eugene Aug 09 '12 at 10:33 2012-08-09 10:33
source share