Home spring boot many to many doesn't work
Reply: 0

spring boot many to many doesn't work

Toni
1#
Toni Published in 2017-12-06 12:11:40Z

I have the following problem in spring boot:

I have a web when add solicitants objects and edit and save, I get this error:

java.lang.IllegalStateException: MergeContext#attempt to create managed -> managed mapping with different entities: [com.gestio_deutes_hereus.entity.Solicitant#1]; [com.gestio_deutes_hereus.entity.Solicitant#1]

I have a relation many to many between Entities Soliciting and Beneficiari:

//Solicitant:

     @ManyToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE})
        @JoinTable(name="beneficiari_solicitant",joinColumns = {@JoinColumn(name="solicitant_id")},inverseJoinColumns = {@JoinColumn(name="beneficiari_id")})
        private List<Beneficiari> solicitants=new ArrayList<>();

    @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((id == null) ? 0 : id.hashCode());

            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (!(obj instanceof Solicitant))
                return false;
            Solicitant other = (Solicitant) obj;
            if (id == null) {
                if (other.id != null)
                    return false;
            } else if (!id.equals(other.id))
                return false;

            return true;
        }


   // Beneficiari:

     @ManyToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE})
            @JoinTable(name="beneficiari_solicitant",joinColumns = {@JoinColumn(name="beneficiari_id")},inverseJoinColumns = {@JoinColumn(name="solicitant_id")})
        private List<Solicitant> solicitants=new ArrayList<>();
     @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((id == null) ? 0 : id.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (!(obj instanceof Beneficiari))
                return false;
            Beneficiari other = (Beneficiari) obj;
            if (id == null) {
                if (other.id != null)
                    return false;
            } else if (!id.equals(other.id))
                return false;

            return true;
        }enter code here

And my SolicitantController is:

 @RequestMapping(value="/guarda" ,method= RequestMethod.POST)
    public String save(@Valid Solicitant solicitant, BindingResult result, Model model) throws ParseException {

        if (result.hasErrors()) {
            model.addAttribute("parentius",parentiuSolicitantService.list());
            model.addAttribute("estatsSolicituts",estatSolicitudService.list());
            return "solicitant/crear";
        } else {
            if(beneficiari!=null){ //Si es una entrada nova
                Solicitant solicitantSaved = solicitantService.save(solicitant);
                beneficiari.getSolicitants().add(solicitantSaved);
                beneficiariService.save(beneficiari);
            }else {
                Solicitant solicitantSaved = solicitantService.save(solicitant);
            }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299253 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO