'Final field is mutable. What is my problem?
I'm using mybatis and jackson-databind
And I tried to calculate some values in constructor of result type dto
Unfortunately, getter returns original values, not calculated values
I also tried to using @JsonCreator, @JsonProperty, etc...
But getter had return original value
Interestingly, When parameter names of constructor was changed(or sql alias), getter returned calculated value
I guess jackson or mybatis overrides something(or java reflection problem?)
How can I solve this problem without change some names?
This is my query
<select id="getVoteResult" resultType="VoteResultDto">
SELECT SUM(good) AS voteCountOfGood
, SUM(bad) AS voteCountOfBad
FROM some.recommend_inf
WHERE id = #{id};
</select>
and result dto
public final class VoteResultDto {
private final int voteCountOfGood;
private final int voteCountOfBad;
public VoteResultDto(float voteCountOfGood, float voteCountOfBad) {
System.out.println("float voteCountOfGood: " + voteCountOfGood);
System.out.println("float voteCountOfBad: " + voteCountOfBad);
if (voteCountOfGood > 0 || voteCountOfBad > 0) {
this.voteCountOfGood = Math.round(voteCountOfGood / (voteCountOfGood + voteCountOfBad) * 100);
this.voteCountOfBad = 100 - this.voteCountOfGood;
} else {
this.voteCountOfGood = this.voteCountOfBad = 0;
}
System.out.println("this.voteCountOfGood: " + this.voteCountOfGood);
System.out.println("this.voteCountOfBad: " + this.voteCountOfBad);
}
public int getVoteCountOfGood() {
return voteCountOfGood;
}
public int getVoteCountOfBad() {
return voteCountOfBad;
}
@Override
public String toString() {
return "toString(): {voteCountOfGood: " + voteCountOfGood + ", voteCountOfBad: " + voteCountOfBad + '}';
}
}
and log
float voteCountOfGood: 8.0
float voteCountOfBad: 4.0
this.voteCountOfGood: 67
this.voteCountOfBad: 33
toString(): {voteCountOfGood: 8, voteCountOfBad: 4}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
