0
我想使用Spring-JPA更新與主鍵關聯的記錄。如何在Spring中更新與主鍵關聯的值-JPA
GroupChatHeartBeat groupChatHeartBeat=new GroupChatHeartBeat();
groupChatHeartBeat.setId(user.getId());
groupChatHeartBeat.setGender(user.getGender());
groupChatHeartBeat.setHeartBeatTime(new Date());
groupChatHeartBeat.setUrl(userPhoto.getSrcBig());
groupChatHeartBeatRepository.save(groupChatHeartBeat);
其中GroupChatHeartBeat被聲明爲實體,但是這樣做並沒有用新值替換。它只顯示舊的價值。我的意圖是更新表,如果存在一個ID它應該被新的記錄(如時間,性別,網址等)取代。這裏是實體
@Entity
@Table
public class GroupChatHeartBeat implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
private Date heartBeatTime;
private String url;
private Gender gender;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Date getHeartBeatTime() {
return heartBeatTime;
}
public void setHeartBeatTime(Date heartBeatTime) {
this.heartBeatTime = heartBeatTime;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}