java - How to cascade parent entity when persisting child in bidirectional -


i have child class called producto looks this:

@entity public class producto implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     private int id;      @notnull(message = "por favor ingrese un valor")     private string nombre;      @notnull(message = "por favor ingrese un valor")     @column(length = 140)     private string descripcion;      @notnull(message = "por favor ingrese un valor")     private double precio;      @notnull(message = "por favor ingrese un valor")     private string imagen1;     @notnull(message = "por favor ingrese un valor")     private string imagen2;     @notnull(message = "por favor ingrese un valor")     private string imagen3;      private int megusta;     private int nomegusta;       @onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true)     @joincolumn(name = "comentario_producto", referencedcolumnname = "id")     private list<comentario> comentario = new arraylist<comentario>();//       @manytoone(cascade = cascadetype.all, fetch = fetchtype.eager)     private subcategoria subcategoria = new subcategoria();       @manytoone(cascade = cascadetype.all, fetch = fetchtype.eager)     private categoria categoria = new categoria();       @manytoone(cascade = cascadetype.all, fetch = fetchtype.eager)     private sucursal sucursal;       // getters , setters 

the parent class subcategoria looks this:

@entity public class subcategoria implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     private int id;      @notnull(message = "por favor ingrese un valor")     @column(length = 100)     private string nombre;      @onetomany(cascade = cascadetype.persist, fetch = fetchtype.eager, orphanremoval = false, mappedby = "subcategoria")     private list<producto> producto = new arraylist<producto>();      // getters , setters 

my child entity producto has parent categoria entity looks this:

@entity public class categoria implements serializable {     @notnull     @id     @generatedvalue(strategy = generationtype.auto)     private int id;      @notnull(message = "por favor ingrese un valor")     @column(length = 100)     private string nombre;      @onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true)     @joincolumn(name = "subcategoria_categoria", referencedcolumnname = "id")     private list<subcategoria> subcategoria = new arraylist<subcategoria>();      @onetomany(cascade = cascadetype.persist, fetch = fetchtype.eager, orphanremoval = false, mappedby = "categoria")     private list<producto> producto; 

the entity producto has parent called sucursal looks this:

@entity public class sucursal implements serializable {      @id     @generatedvalue(strategy = generationtype.auto)     private int id;      private string subgerente;      @notnull(message = "por favor ingrese un valor en direccion")     @column(length = 120)     private string direccion;      @notnull(message = "por favor ingrese un valor en telefono")     @column(length = 36, unique = true)     private string telefono;      @onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true, mappedby = "sucursal")     private list<producto> producto; //getters , setters 

this save method looks on managed bean persists childs producto parent sucursal:

public void guardar() {      list<producto> prods = new arraylist<producto>();     (int = 0; < listproductos.size(); i++) {          producto p = new producto();          p.setid(listproductos.get(i).getid());         p.setnombre(listproductos.get(i).getnombre());          p.setdescripcion(listproductos.get(i).getdescripcion());         p.setimagen1(listproductos.get(i).getimagen1());         p.setimagen2(listproductos.get(i).getimagen2());         p.setimagen3(listproductos.get(i).getimagen3());         p.setprecio(listproductos.get(i).getprecio());         p.setsucursal(newsucursal);          p.setcategoria(categoriadao.read(listproductos.get(i).getcategoria().getid()));         p.setsubcategoria(subcategoriadao.read(listproductos.get(i).getsubcategoria().getid()));          prods.add(p);         productodao.save(p);      }      newsucursal.setproductos(prods);      sucursaldao.save(newsucursal);  } 

the logic on managed bean use @postconstruct annotation load entity sucursal on jsf page have list of producto belongs parent entity sucursal. have save method persits entity sucursal corresponding child entities producto , update fk other parents entities of producto (categoria , subcategoria). problem whenever persist entity producto gets updated last fk list producto mentioned above. question how can update parent fk of subcategoria , categoria on child entity producto each child entity producto belongs parent sucursal?

you need make sucursal parent entity. add associations subcategoria , categoria on producto entity , add producto entity sucursal entity.

on saving new sucursal entity cascade handle rest in persisting producto entity , adding associations.

    sucursal newsucursal = new sucursal();     newsucursal.setsubgerente("abc");      int size = 2;     (int = 0; < size; i++) {          producto p = new producto();         p.setdescripcion("description " + i);          //add associations         categoria categoria = em.find(categoria.class, 1);         p.setcategoria(categoria);         subcategoria subcategoria = em.find(subcategoria.class, 1);         p.setsubcategoria(subcategoria);          //add new sucursal         newsucursal.addproduct(p);     }      sucursaldao.save(newsucursal); 

on sucursal entity

@onetomany(cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true, mappedby = "sucursal") private list<producto> producto = new arraylist<>();  public void addproduct(producto p) {     p.setsucursal(this);     this.producto.add(p); } 

the code above execute following sql statements

insert sucursal (id, subgerente) values (1, 'abc') insert producto (id, categoria_id, descripcion, subcategoria_id, sucursal_id) values (1, 1, 'description 1', 1, 1) insert producto (id, categoria_id, descripcion, subcategoria_id, sucursal_id) values (2, 1, 'description 2', 1, 1) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -