2016-02-04 46 views
0

我收到以下錯誤每當我試圖堅持一個子對象到現有的父親對象JPA:ConstraintViolationException當試圖堅持

產生的原因:javax.validation.ConstraintViolationException:驗證失敗等級[店.entidades.Sucursal]在組持續時間期間[javax.validation.groups.Default,] 約束違規列表:ConstraintViolationImpl {interpolatedMessage ='Por favour ingrese un valor en direccion',propertyPath = direccion,rootBeanClass = class store .entidades.Sucursal,messageTemplate ='Por favor ingrese un valor en direccion'} ConstraintViolationImpl {interpolatedMessage ='Por favor ingrese未勇氣烯telefono ingrese未英勇烯telefono '} ]」,的PropertyPath = telefono,rootBeanClass =類store.entidades.Sucursal,messageTemplate =' POR青睞

這是即時通訊使用父實體類:

@Entity 
    public class Sucursal { 

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

     // nombre del encargado de esa sucursal 
     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.MERGE, fetch = FetchType.EAGER, orphanRemoval = true) 
     @JoinColumn(name = "producto_sucursal", referencedColumnName = "id") 
     private List<Producto> producto = new ArrayList<Producto>(); 

     //getters and setters 

這是孩子的實體類,即時通訊使用:

@Entity 
    public class Producto { 

    @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.MERGE, fetch = FetchType.EAGER) 
    @JoinColumn(name = "producto_subcategoria") 
    private Subcategoria subcategoria = new Subcategoria(); 

    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) 
    @JoinColumn(name = "producto_categoria") 
    private Categoria categoria = new Categoria(); 

    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) 
    @JoinColumn(name = "producto_sucursal") 
    private Sucursal sucursal = new Sucursal(); 

    //getters and setters 

我對Sucursal DAO類看起來是這樣的:

@Stateless 
public class SucursalDAO implements Serializable { 

    @Inject 
    private EntityManager entityManager; 

    public void save(Sucursal sucursal) { 
     if (entityManager.find(Sucursal.class, sucursal.getId()) == null) { 
      insertar(sucursal); 

     } else { 
      update(sucursal); 
     } 
    } 

    // ... 
} 

每當我點擊在前端的保存按鈕,它會調用這個方法:

public void guardar() { 

    System.out.println(newSucursal.getDireccion()); 
    System.out.println(newSucursal.getTelefono()); 

    for (int i = 0; i < listproductos.size(); i++) { 

     // listproductos.get(i).setSucursal(newSucursal); 
     System.out.println(listproductos.get(i).toString()); 

     newSucursal.getProductos().add(listproductos.get(i)); 
    } 

    sucursalDAO.save(newSucursal); 
} 

當我把這種節約方法我添加了新的子PRODUCTOSSucursales但我當嘗試堅持父母給它我上面發佈的錯誤。 我已經嘗試了很多東西,但似乎無法弄清楚是什麼問題? 是否有另一種將子實體保存到現有父實體的方法?

編輯

這是固定我的問題對我的保存方法的代碼:

public void guardar() { 

     System.out.println(newSucursal.getDireccion()); 
     System.out.println(newSucursal.getTelefono()); 

     for (int i = 0; i < listproductos.size(); i++) { 

      listproductos.get(i).setSucursal(newSucursal); 

      Categoria cat = new Categoria(); 
      cat = categoriaDAO.read(listproductos.get(i).getCategoria().getId()); 
      listproductos.get(i).setCategoria(cat); 

      Subcategoria subcat = new Subcategoria(); 
      subcat = subcategoriaDAO.read(listproductos.get(i).getSubcategoria().getId()); 
      listproductos.get(i).setSubcategoria(subcat); 

      System.out.println(listproductos.get(i).toString()); 

      newSucursal.getProductos().add(listproductos.get(i)); 
     } 

     // newSucursal.setProductos(listproductos); 

     sucursalDAO.save(newSucursal); 
    } 
+0

如果我使用postconstruct從數據庫中獲取值,那麼值怎麼會變空?我甚至在我調用save方法之前將它們打印在屏幕上,並且它們不爲null –

+0

您的'@ ViewScoped'類和'@ Entity'類不實現'Serializable'。 – Geinmachi

+0

我加了'implements Serializable',還是一樣的錯誤。 –

回答

1

你的問題的關鍵是,你需要指定你的人際關係兩側:

public void guardar() { 

    System.out.println(newSucursal.getDireccion()); 
    System.out.println(newSucursal.getTelefono()); 

    for (int i = 0; i < listproductos.size(); i++) { 

     listproductos.get(i).setSucursal(newSucursal); 
     System.out.println(listproductos.get(i).toString()); 

     newSucursal.getProductos().add(listproductos.get(i)); 
    } 

    sucursalDAO.save(newSucursal); 
} 

因爲某些原因你已經註釋掉了。

而且,所有的@ManyToOne關係被初始化與像新的對象:

@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) 
@JoinColumn(name = "producto_subcategoria") 
private Subcategoria subcategoria = new Subcategoria(); 

這是自找麻煩,因爲(再次)這些關係的兩側永遠不會置。

+0

啊啊是的,它飛過我的頭,當我使用雙向我必須從雙方分配,非常感謝!這解決了我的問題。我會用代碼來編輯我的問題,以防別人需要它。 –