title: Assign a list<T> to a variable in javascript using thymeleaf
body: <p>I'm trying to get a list of objects in a variable in javascript, added to model in the controller. As a test, I passed a list of Strings and it worked fine but it doesn't work with the list of objects. Can anybody help me please? I've been searching for a solution but I can't find anything.</p>
  
  <p>This is the controller</p>
  
  <pre><code>@GetMapping(value= "/view_dates")
  public String viewDates(Model model){
      List&lt;Division&gt; list =divisionService.getActiveDates();
      model.addAttribute("dates", list);
      return  "course_view_dates";
  }
  </code></pre>
  
  <p>and this is the javascript code</p>
  
  <pre><code>   &lt;script th:inline = "javascript"&gt;
          /*&lt;![CDATA[*/
              var examDates = /*[[${dates}]]*/ null;
              for (i=0; i&lt; examDates.length; i++){
                  alert(examDates[i].name);
              }
          /*]]&gt;*/
      &lt;/script&gt;
  </code></pre>
  
  <p>This is the entity for more information</p>
  
  <pre><code>@Entity
  @Table(name="division")
  public class Division implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)  
    private Integer id;
    @OneToMany(mappedBy = "division")
    private List&lt;Kind&gt; kinds;
    @JsonIgnore
    @OneToMany(mappedBy = "division", cascade = CascadeType.ALL)
    private List&lt;Sample&gt; samples;
    @ManyToMany(mappedBy = "divisions", cascade = CascadeType.ALL)
    private List&lt;Candidate&gt; candidates;
    @Column(name = "name", nullable = false)
    private String name;
    @Column(name = "description", nullable = true)
    private String description;
    @Column(name = "course_days", nullable = true)
    private Integer courseDays;
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern= "dd/MM/yyy")
    @Column(name = "exam_date", nullable = true)
    private Date examDate;
    @Column(name = "active", nullable = true)
    private Boolean active;
  
    public Division() {
      this.name = "";
      this.description = "";
      this.courseDays = 0;
      this.examDate = null;
      this.active = false;
    }
  
  public Division(String name, String description, Integer courseDays, Date examDate, Boolean dateActive){
      this.name = name;
      this.description = description;
      this.courseDays = courseDays;
      this.examDate = examDate;
      this.active = dateActive;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public Integer getCourseDays() {
      return courseDays;
  }
  
  public void setCourseDays(Integer courseDays) {
      this.courseDays = courseDays;
  }
  
  public List&lt;Sample&gt; getSamples() {
      return this.samples;
  }
  
  public void setSamples(List&lt;Sample&gt; samples) {
      this.samples = samples;
  }
  
  public Date getExamDate() {
      return examDate;
  }
  
  public void setExamDate(Date ExamDate) {
      this.examDate = ExamDate;
  }
  
  public Boolean getActive() {
      return active;
  }
  
  public void setActive(Boolean active) {
      this.active = active;
  }
  
  public List&lt;Kind&gt; getKinds() {
      return this.kinds;
  }
  
  public void setKinds(List&lt;Kind&gt; varieties) {
      this.kinds = varieties;
  }
  
  public List&lt;Candidate&gt; getCandidates() {
      return this.candidates;
  }
  
  public void setCandidates(List&lt;Candidate&gt; candidates) {
      this.candidates = candidates;
  } 
  </code></pre>
  
  <p>It produces two calls to the database and then the ERROR  </p>
  
  <pre><code>Hibernate: select candidates0_.division_id as division2_1_0_,             candidates0_.candidate_id as candidat1_1_0_, candidate1_.id as id1_0_1_, candidate1_.name as name2_0_1_ from ostscourses.candidate_division candidates0_ inner join ostscourses.candidate candidate1_ on candidates0_.candidate_id=candidate1_.id where candidates0_.division_id=?
  Hibernate: select divisions0_.candidate_id as candidat1_1_0_, divisions0_.division_id as division2_1_0_, division1_.id as id1_2_1_, division1_.active as active2_2_1_, division1_.course_days as course_d3_2_1_, division1_.description as descript4_2_1_, division1_.exam_date as exam_dat5_2_1_, division1_.name as name6_2_1_ from ostscourses.candidate_division divisions0_ inner join ostscourses.division division1_ on divisions0_.division_id=division1_.id where divisions0_.candidate_id=? order by division1_.name asc
  2017-06-07 10:55:19.994 ERROR 5576 --- [pr-8080-exec-17] o.s.boot.web.support.ErrorPageFilter     : Forwarding to error page from request [/course/view_dates/] due to exception [null]
  
  java.lang.StackOverflowError: null
  at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:562) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
  at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
  at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:277) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
  at org.thymeleaf.util.JavaScriptUtils.printCollection(JavaScriptUtils.java:316) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
  </code></pre>