2016-09-09 83 views
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; 
    } 

} 

回答

1

試試這個:

要更新數據庫中現有的實體 - 你必須新對象的對象ID設置的OLDobject標識。

newObject.setId(OldObject.getId())然後repo.save(newObject)將更新數據庫中的現有實體。

實體類

@Entity 
public class GroupChatHeartBeat { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    private Date heartBeatTime; 

    private String url; 

    public GroupChatHeartBeat() { 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public Date getHeartBeatTime() { 
     return heartBeatTime; 
    } 

    public void setHeartBeatTime(Date heartBeatTime) { 
     this.heartBeatTime = heartBeatTime; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public GroupChatHeartBeat(Date heartBeatTime, String url) { 
     this.heartBeatTime = heartBeatTime; 
     this.url = url; 
    } 

    @Override 
    public String toString() { 
     return "GroupChatHeartBeat{" + 
       "id=" + id + 
       ", heartBeatTime=" + heartBeatTime + 
       ", url='" + url + '\'' + 
       '}'; 
    } 
} 

自動裝配庫

@Autowired 
private GroupChatHeartBeatRepository groupChatHeartBeatRepository; 

保存和更新

// Save New 
GroupChatHeartBeat grp = new GroupChatHeartBeat(new Date(), "http://www.google.com"); 
groupChatHeartBeatRepository.save(grp); 

groupChatHeartBeatRepository.findAll().forEach(System.out::println); 

// Update same object and save-> updates value of existing in database 
grp.setUrl("http://spring.io"); 
groupChatHeartBeatRepository.save(grp); 

groupChatHeartBeatRepository.findAll().forEach(System.out::println); 

// Create New object, set Id of new object as old object and save-> updates value of existing in database 
GroupChatHeartBeat grpUpdated = new GroupChatHeartBeat(new Date(638893800000L), "https://github.com/RawSanj"); 
grpUpdated.setId(grp.getId()); 
groupChatHeartBeatRepository.save(grpUpdated); 

groupChatHeartBeatRepository.findAll().forEach(System.out::println); 

在我的GitHub倉庫中籤出Complete Project