skip to Main Content

I have tree models ‘cinema’, ‘cinemaHall’ and ‘seo’, where ‘cinema’ has relationship @OneToMany to ‘cinemaHall’. Also, ‘cinema’ and ‘cinemaHall’ models have relationship @OneToOne to ‘seo’. The problem is in model seo. After creating ‘cinema’ and ‘cinemaHall’ I’m getting, the same ‘seo’ from Thymeleaf, which is related to ‘cinema’. But I need to received ‘seo’ (for example, with id = 2) for ‘cinemaHall’. Please, find the screenshot for understanding.

CinemaController.java:

...
@PutMapping("update/{cinemaId}")
    public String updateCinema(@PathVariable("cinemaId") Long cinemaId,
                               @ModelAttribute("cinema") Cinema cinema,
                               @ModelAttribute("seo") SEO seo){
        cinema.setSeo(seo);
        cinemaService.updateCinema(cinema);
        return "redirect:/admin/cinemas";
    }
...
 @PutMapping("edit/{cinemaId}/halls/update/{hallId}")
    public String updateCinemaHall(@PathVariable("cinemaId") Long cinemaId,
                                   @PathVariable("hallId") Long hallId,
                                   @ModelAttribute("cinemaHall") CinemaHall cinemaHall,
                                   @ModelAttribute("seo") SEO seo){
        cinemaHall.setSeo(seo);
        cinemaHallService.updateCinemaHall(cinemaHall);
        return "redirect:/admin/cinemas/edit/" + cinemaId;
    }
...

Cinema.java

...
@OneToMany
    @JoinColumn(name="cinema_id", referencedColumnName="id")
    private List<CinemaHall> cinemaHallList = new ArrayList<>();
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "seo_id")
    private SEO seo;
...

CinemaHall.java

...
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "seo_id")
    private SEO seo;
...

SEO.java

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "seo")
public class SEO extends MappedEntity {

    @Column
    private String url;
    @Column
    private String title;
    @Column
    private String keywords;
    @Column(name = "seo_description")
    private String seoDescription;

}

After creating, for example, cinema(id=1) and seo(id=1) I’m trying to create cinemaHall(id=1) while seo(id=2) was successfully created too, but after applying action for saving cinemaHall I’m receiving seo with id=1 and fields like it has from id=2.
Look at seo which has id=1, but should be id=2

2

Answers


  1. Actually your given relationship is not working possibility.because A to B relationship is fine and A to C Or B to C any One relationship only possible both are not possible.

    A and B Both need relationship to C then no need of A to B relationship.If B have relation with C then A can have access to to C Object from respective B object.

    Login or Signup to reply.
  2. You don’t need the one to many mapping between cinema and cinemaHall if u have one-to-one mapping between cinema and seo AND cinemaHall and seo.

    just use one to one mapping and this should resolve your issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search