Warm tip: This article is reproduced from stackoverflow.com, please click
java jpa spring-boot Thymeleaf

Consolidating an One to Many relationship in Thymeleaf

发布于 2020-04-13 09:36:24

I am trying to consolidate an One to Many relationship using Thymeleaf. Let's keep it simple. I do have a thiesis a thiesis has some questions.

@Entity
public class Thiesis {

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

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="thiesis")
    private Set<Question> questions = new HashSet<>();

    @ManyToOne(cascade=CascadeType.ALL)
    private Course course;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;
}

And the question class:

@Entity
public class Question {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private String question;
  @ManyToOne(cascade=CascadeType.ALL)
  private Thiesis thiesis;
}

And here is the controller for saving the thiesis:

@PostMapping("/teacher/{userId}")
public String addThiesis(@PathVariable Long userId, Thiesis thiesis,String name)
{
    thiesisService.save(thiesis, name);
    System.out.println(thiesis.getId());
    return "redirect:/teacher/" + userId.toString();
}

Also the service for Thiesis:

public Thiesis save(Thiesis thiesis, String name)
{
    Course course =courseRepo.findByName(name);
    if(course!=null) {
        thiesis.setCourse(course);
        for(Question question : thiesis.getQuestions()) {
            question.setThiesis(thiesis);
            questionService.save(question);
        }
        Date date = new Date();
        thiesis.setCreatedAt(new Timestamp(date.getTime()));

        return thiesisRepo.save(thiesis);

    }
    else {
        System.out.println("Couldn't save the thiesis");
        return null;
    }

Thymeleaf:

<form action="" th:object="${thiesis}"method="post">
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" required/>
    <div class="form-group row" id="course_id">
        <label for="pyetja_1" class="col-12 col-sm-4 col-form-label">Pyetje:</label>
        <div class=" col-12 col-sm-8">  
            <input type="text" class="form-control" placeholder="Emri i Lendes..." th:field="*{course.name}" required/><br/>
        </div>                      
    </div>
</form>

But the result is not getting into database, and as a response I am getting a 403 error, when I perform the POST method. Probably the problem is at the way I am completing the object fields in thymeleaf. I am really stuck, and I have tried thousands of ways and I am not dealing with it. Any help is much appreciated.

Questioner
dprozz122
Viewed
108
Shpend Palushi 2020-02-03 07:00

From what I can see and understand from your naming conventions, probably you are messing up the situation with the Mappings. That is supposed to happen in these situations. The posted code should include also the @GetMapping, but anyways make sure that the @GetMapping is pointing to that view, and the @PostMapping is getting the info from that view in the specified path that you are referring to.