0

So I'm using Spring MVC 4 and I'm having issues with an ArrayList and I need to convert it to a set to display it, because when I try to display it as a list it contains duplicates and it lists out a lot of repeating data, which I don't want. And I can't just change the overall type to a set because I have a dynamic form that doesn't allow it.

I'll show some of my code and if anyone has any ideas on how I can get this to work that would be super helpful. Thanks in advance.

So here's my parent object which is named StudySet, and it contains a list of Rows.

@Entity
public class StudySet {
private Long id;
private String title;
private List <Row> rows = new ArrayList<Row>();;
private User user;

@Id
@GeneratedValue
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="studySet", orphanRemoval=true)
public List<Row> getRows() {
    return rows;
}
public void setRows(List<Row> rows) {
    this.rows = rows;
}
@ManyToOne
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public void initialize(){

    Row item1 = new Row();

    this.rows.add(item1);    
}
}

Here's my Row object

@Entity
public class Row {
private Long id;
private String question;
private String answer;
private StudySet studySet;

public Row(){}

@Id
@GeneratedValue
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getQuestion() {
    return question;
}
public void setQuestion(String question) {
    this.question = question;
}
public String getAnswer() {
    return answer;
}
public void setAnswer(String answer) {
    this.answer = answer;
}
@ManyToOne
public StudySet getStudySet() {
    return studySet;
}
public void setStudySet(StudySet studySet) {
    this.studySet = studySet;
}
public Row(Long id, String question, String answer, StudySet studySet) {
    this.id = id;
    this.question = question;
    this.answer = answer;
    this.studySet = studySet;
}   

}

Here's my Controller

@Controller
public class StudySetController {
private StudySetRepository studySetRepo;

@RequestMapping(value="studySet/{studySetId}", method=RequestMethod.GET)
public String addPostGet (@PathVariable Long studySetId, ModelMap model)
{
    StudySet studySet = studySetRepo.findOne(studySetId);
    model.put("studySet", studySet);
    List <Row> rows = studySet.getRows();
    model.put("rows", rows);

    return "studySet";
}
@Autowired
public void studySetRepo(StudySetRepository studySetRepo) {
    this.studySetRepo = studySetRepo;
}
}

I don't really have a service layer for this part, so I was hoping that there would be someway to convert the list into a set somewhere in here so that I could display it properly without duplicates, and then I could put it on the model.

Also I have tested it as a set, and it displays just like I want to do, but like I said earlier, I have a form that adds multiple rows at once to a studySet that needs to be an arraylist.

dochsner
  • 249
  • 2
  • 8
  • 25

0 Answers0