如何在Spring-JPA中更新与主键关联的值

时间:2016-09-09 11:16:25

标签: spring hibernate spring-data spring-data-jpa hibernate-mapping

我想使用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被声明为Entity,但这样做并不会替换为新值。它只显示旧的价值。我的目的是更新表格,如果存在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 个答案:

答案 0 :(得分:2)

试试这个:

更新数据库中的现有实体 - 必须将新Object的Object Id设置为OldObject Id。

即。 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 + '\'' +
                '}';
    }
}

Autowire Repository

@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