'String attribute of entity is not being updated properly
I have an entity in my Spring application called Events that has a String attribute called "registrants". This is essentially a list of names of user signed up for the event. When a user is deactivated, I call the removeRegistrant method in the class to replace the name of the user removed with the empty String. However, when I call this method, nothing changes, and registrants remains the same. I can call addRegistrant just fine and registrants is updated appropriately, so I am not sure what the issue is here. Do I need a tag above registrants for things to be refreshed/updated properly here? See the Java class for events here:
import java.time.LocalDate;
import java.time.LocalTime;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import ymca.tracker.application.data.AbstractEntity;
@Embeddable
@Entity
public class Events extends AbstractEntity {
private String name;
private java.time.LocalDate startDate;
private java.time.LocalDate endDate;
private java.time.LocalTime startTime;
private java.time.LocalTime endTime;
private int participants;
private int nonMemberPrice;
private int memberPrice;
private String location;
private String description;
private String registrants;
private boolean[] days;
private Status status;
public Events() {
}
public Events(String name, LocalDate startDate, LocalDate endDate, LocalTime startTime, LocalTime endTime, int participants,
int nonMemberPrice, int memberPrice, String location, String description, String registrants, boolean[] days) {
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.startTime = startTime;
this.endTime = endTime;
this.participants = participants;
this.nonMemberPrice = nonMemberPrice;
this.memberPrice = memberPrice;
this.location = location;
this.description = description;
this.registrants = registrants;
this.days = days;
this.status = Status.ACTIVE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.time.LocalDate getStartDate() {
return startDate;
}
public void setStartDate(java.time.LocalDate startDate) {
this.startDate = startDate;
}
public java.time.LocalDate getEndDate() {
return endDate;
}
public void setEndDate(java.time.LocalDate endDate) {
this.endDate = endDate;
}
public java.time.LocalTime getStartTime() {
return startTime;
}
public void setStartTime(java.time.LocalTime startTime) {
this.startTime = startTime;
}
public java.time.LocalTime getEndTime() {
return endTime;
}
public void setEndTime(java.time.LocalTime endTime) {
this.endTime = endTime;
}
public int getParticipants() {
return participants;
}
public void setParticipants(int participants) {
this.participants = participants;
}
public void decreaseCapacity(int numToDecreaseBy) {
this.participants = this.participants - numToDecreaseBy;
}
public void increaseCapacity(int numToIncreaseBy) {
this.participants = this.participants + numToIncreaseBy;
}
public int getMemberPrice() {
return memberPrice;
}
public void setMemberPrice(int memberPrice) {
this.memberPrice = memberPrice;
}
public int getNonMemberPrice() {
return nonMemberPrice;
}
public void setNonMemberPrice(int nonMemberPrice) {
this.nonMemberPrice = nonMemberPrice;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRegistrants() {
return registrants;
}
public void setRegistrants(String registrants) {
this.registrants = registrants;
}
public void addRegistrant(String user) {
this.registrants += user;
}
public void removeRegistrant(String user) {
String newRegistrants = this.registrants;
newRegistrants.replaceAll(user, "");
this.registrants = newRegistrants;
}
public boolean checkRegistrant(String user) {
return this.registrants.contains(user);
}
public void setDays(boolean[] days) {
this.days = days;
}
public boolean[] getDays() {
return days;
}
public String retrieveDays() {
String toReturn = "";
for(int i = 0; i < days.length; i++) {
if(days[i] == true) {
if(i == 0) {toReturn += "Sunday, ";}
if(i == 1) {toReturn += "Monday, ";}
if(i == 2) {toReturn += "Tuesday, ";}
if(i == 3) {toReturn += "Wednesday, ";}
if(i == 4) {toReturn += "Thursday, ";}
if(i == 5) {toReturn += "Friday, ";}
if(i == 6) {toReturn += "Saturday, ";}
}
}
toReturn = toReturn.substring(0, toReturn.length() - 2);
return toReturn;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
Solution 1:[1]
Strings are immutable, so the replaceAll methods returns a different object, it does not modify the source variable.
Your removeRegistrant should assign to registrants the return vslue of replaceAll
this.registrants = this.registrants.replaceAll(user, "")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marco C |
