Java热插拔失败并且未实现架构更改

时间:2019-04-11 19:09:03

标签: java rest spring-boot curl

我在Java Spring引导应用程序上工作,在该应用程序中我收到热插拔失败的错误,并且未实现架构更改,并且VM不支持该操作。之后,该表将被截断并且根本没有数据。

enter image description here

我在下面提供了2种型号,

@Entity
public class User {

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

    @Column(name = "name")
    @NotNull
    @NotEmpty
    private String name;

    @Column(name = "countryName")
    @NotNull
    @NotEmpty
    private String countryName;


    @Column(name = "currencyName")
    @NotNull
    @NotEmpty
    private String currencyName;


    /*
     * total steps is for the keepign the history of the user movement
     * */
    @Column(name = "totalSteps")
    @Min(value = 0L, message = "The value must be positive")
    private int totalSteps;

    /*
     * current steps is for providing the user reward. We will need to set
     * it to zero after processing the user payment
     * */
    @Column(name = "currentSteps")
    @Min(value = 0L, message = "The value must be positive")
    private int currentSteps;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RewardList> rewardLists = new ArrayList<>();

    public User() {

    }

    public User(@NotNull @NotEmpty String name, @NotNull @NotEmpty String countryName) {
        this.name = name;
        this.countryName = countryName;
    }

    public User(@NotNull @NotEmpty String name, @NotNull @NotEmpty String countryName, @Min(value = 0L, message = "The value must be positive") int totalSteps) {
        this.name = name;
        this.countryName = countryName;
        this.totalSteps = totalSteps;
    }


    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountryName() {
        return countryName;
    }

    public String getCurrencyName() {
        return currencyName;
    }

    public void setCurrencyName(String currencyName) {
        this.currencyName = currencyName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public int getTotalSteps() {
        return totalSteps;
    }

    public void setTotalSteps(int totalSteps) {
        this.totalSteps = totalSteps;
    }

    public int getCurrentSteps() {
        return currentSteps;
    }

    public void setCurrentSteps(int currentSteps) {
        this.currentSteps = currentSteps;
    }

    public List<RewardList> getRewardLists() {
        return rewardLists;
    }

    public void setRewardLists(RewardList rl) {
        this.rewardLists.add(rl);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return getTotalSteps() == user.getTotalSteps() &&
                getCurrentSteps() == user.getCurrentSteps() &&
                getId().equals(user.getId()) &&
                getName().equals(user.getName()) &&
                getCountryName().equals(user.getCountryName()) &&
                getRewardLists().equals(user.getRewardLists());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getName(), getCountryName(), getTotalSteps(), getCurrentSteps(), getRewardLists());
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", countryName='" + countryName + '\'' +
                ", totalSteps=" + totalSteps +
                ", currentSteps=" + currentSteps +
                '}';
    }
}


@Entity
public class RewardList {

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

    @Column(name = "reward")
    @Min(value = 0L, message = "The value must be positive")
    private double reward;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    public RewardList() {
    }

    public RewardList(User user) {
        this.user = user;
    }

    public RewardList(@Min(value = 0L, message = "The value must be positive") double reward) {
        this.reward = reward;
    }


    public Long getId() {
        return id;
    }

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

    public double getReward() {
        return reward;
    }

    public void setReward(double reward) {
        this.reward = reward;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof RewardList)) return false;
        RewardList list = (RewardList) o;
        return Double.compare(list.getReward(), getReward()) == 0 &&
                getId().equals(list.getId()) &&
                getUser().equals(list.getUser());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getReward(), getUser());
    }


    @Override
    public String toString() {
        return "RewardList{" +
                "id=" + id +
                ", reward=" + reward +
                ", user=" + user +
                '}';
    }
}

我在下面提供此问题的端点

// $ curl -X PUT http://localhost:8080/api/v1/users/calculateReward?userId=1 | jq

@PutMapping("/calculateReward")
public ResponseEntity<Object> calculateReward(@RequestParam("userId") Long userId) {

    Optional<User> optional = userService.findById(userId);

    if (!optional.isPresent()) {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    User user = optional.get();

    double reward = user.getCurrentSteps() * Parameters.REWARD_PER_STEPS_EUR;

    RewardList list = new RewardList();

    list.setUser(user);
    list.setReward(reward);
    rewardListService.save(list);

    user.setCurrentSteps(0);
    user.setRewardLists(list);
    userService.save(user);

    JSONObject json = new JSONObject();

    double convertionRateToEuro = currencyMap.get(user.getCurrencyName());
    double rewardConverted = reward * convertionRateToEuro;

    json.put("name", user.getName());
    json.put("currency", user.getCurrencyName());
    json.put("reward", rewardConverted);

    return ResponseEntity.status(HttpStatus.CREATED).body(json);
}

有人知道发生了什么事并且可以提供解决方案吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了原因,下面提供了一个解决方案。我们将需要根据层次结构保存模型。 HotSwap不支持错误消息所指示的添加方法或层次结构更改。这是Java HotSwap的限制,而不是IntelliJ IDEA。正确的代码顺序方式将是

    User user = optional.get();

    RewardList list = new RewardList();

    list.setUser(user);
    list.setReward(reward);

    user.setCurrentSteps(0);
    user.setRewardLists(list);


    // first save the User
    userService.save(user);

    // Then, save the RewardList as it has one to many relations
    rewardListService.save(list);