【问题标题】:Saving Objects from an Iterated List从迭代列表中保存对象
【发布时间】:2013-10-21 22:31:39
【问题描述】:

我目前正在使用 java+hibernate+oracle 开发一个销售模块...我在我的 jsp 中完成了我的订单,如下所示:

我正在获取我的参数:

ArrayList<String> idMercaderias = new ArrayList<String>();
ArrayList<String> cantidades = new ArrayList<String>();
ArrayList<String> precios = new ArrayList<String>();
for (int k = 0; k < 10; k++) {
  idMercaderias.add(request.getParameter("idMercaderia" + k));
  cantidades.add(request.getParameter("cantidad" + k));
  precios.add(request.getParameter("precio" + k));
}

我的订单详细信息有 10 行,所以我做了 for,我的输入是 input1、input2、input3 等。这些是我的对象 Mercaderia 的属性,所以我需要设置它们,因为它们在列表:

首先我过滤第一个列表以避免重复文章:

Iterator itra = idMercaderias.listIterator();
ArrayList<String> sortedListIdMercaderias = new ArrayList<String>();
Object m;
while (itra.hasNext()) {
m = itra.next();
  if (!sortedListIdMercaderias.contains(m)) {
  sortedListIdMercaderias.add((String) m);
  }
}

现在我创建我的对象来设置所有属性:

DetallePedido detalle = new DetallePedido();

现在我循环了 10 次(考虑表单中的所有行)并开始迭代每个列表以获取我的对象属性,避免出现空条目。

for (int x = 0; x < sortedListIdMercaderias.size(); x++) {
Iterator itr = idMercaderias.listIterator();
while (itr.hasNext()) {
  String mercaderia = (String) itr.next();
  if ((mercaderia != null) && (!mercaderia.equals(""))) {
    Mercaderia mercaderiaSeleccionada = new MercaderiaDAO().findById(Integer.parseInt(mercaderia));
    detalle.setMercaderia(mercaderiaSeleccionada);
    }
}

Iterator itr2 = cantidades.listIterator();
while (itr2.hasNext()) {
  String cantidad = (String) itr2.next();
  if ((cantidad != null) && (!cantidad.equals(""))) {
    int cantidadMercaderiaSeleccionada = Integer.parseInt(cantidad);
    detalle.setCantidad(cantidadMercaderiaSeleccionada);
    }
}

Iterator itr3 = precios.listIterator();
while (itr3.hasNext()) {
  String precio = (String) itr3.next();
  if ((precio != null) && (!precio.equals(""))) {
    BigDecimal precioMercaderiaSeleccionada = new BigDecimal(precio);
    detalle.setPrecioUnitario(precioMercaderiaSeleccionada);                    
    }
}

最后我只是坚持到我的数据库:

Session session = new DetallePedidoDAO().getSession();
Transaction tx = session.beginTransaction();
try {
  session.saveOrUpdate(detalle);
  tx.commit();
session.close();
} catch (HibernateException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
}

我不知道为什么在数据库中我只插入了 1 行(最后一行有有效数据)而不是全部插入。

非常感谢任何帮助,这是我在大学项目中的期末考试。

【问题讨论】:

  • 请格式化您的代码。没有人会阅读它。
  • interface 和 rawtype 集合的 not 编程还有一个可怕的组合。让皮肤爬行。
  • 对不起,我给代码提供了一些格式。我只是做这些东西的初学者,所以我使用了互联网研究中的东西。

标签: java oracle hibernate list loops


【解决方案1】:

你只有一个DetallePedido 对象。您在各种循环中一遍又一遍地更改其字段值,但它仍然只是一个对象。最后,您正在保存它。就一次。自然,您只会在数据库中插入一行。

您可以尝试的是,不要分别遍历您的Mercaderia 对象、Cantidad 对象和Precio 对象;有一个循环,在每次迭代中创建一个新的DetallePedido 对象,设置MercaderiaCantidadaPrecio,然后保存DetallePedido

【讨论】:

  • 是的,我之前注意到这一点......每个周期都会被覆盖,我该怎么办?
【解决方案2】:

所以,根据David Wallace 的线索,我对这个想法进行了一些调整,并创建了一个对象WrapperDetallePedido,如下所示:

public class WrapperDetallePedido {

    int idMercaderia;
    int cantidad;
    double precio;

    public int getIdMercaderia() {
        return idMercaderia;
    }
    public void setIdMercaderia(int idMercaderia) {
        this.idMercaderia = idMercaderia;
    }
    public int getCantidad() {
        return cantidad;
    }
    public void setCantidad(int cantidad) {
        this.cantidad = cantidad;
    }
    public double getPrecio() {
        return precio;
    }
    public void setPrecio(double precio) {
        this.precio = precio;
    }

}

然后在我的控制器中,我创建了一个 ArrayList 并设置了我的 DetallePedido 属性:

ArrayList<WrapperDetallePedido> listado = new ArrayList<WrapperDetallePedido>();
for (int k = 0; k < 10; k++) {
        if (!request.getParameter("idMercaderia" + k).equals("")){
            WrapperDetallePedido WDetallePedido = new WrapperDetallePedido();
            WDetallePedido.setIdMercaderia(Integer.parseInt(request.getParameter("idMercaderia" + k)));
            WDetallePedido.setCantidad(Integer.parseInt(request.getParameter("cantidad" + k)));
            WDetallePedido.setPrecio(Double.parseDouble(request.getParameter("precio" + k)));
            listado.add(WDetallePedido);
        }                   
    }

最后将Iterator用于上一个列表,并将listado中的所有项目设置并持久化到数据库中:

for (Iterator iterador = listado.listIterator(); iterador.hasNext();) {
    WrapperDetallePedido detalle = (WrapperDetallePedido) iterador.next();
    Mercaderia mercaderiaSeleccionada = new MercaderiaDAO().findById(detalle.getIdMercaderia());
    DetallePedido detallePedido = new DetallePedido();
    detallePedido.setMercaderia(mercaderiaSeleccionada);
    detallePedido.setCantidad(detalle.getCantidad());
    detallePedido.setPrecioUnitario(new BigDecimal(detalle.getPrecio()));
    detallePedido.setPedidos(pedidoGenerado);
    Session session1 = new DetallePedidoDAO().getSession();
    Transaction tx1 = session1.beginTransaction();
    new DetallePedidoDAO().save(detallePedido);
    try {
        session1.saveOrUpdate(detallePedido);
        tx1.commit();
        session1.close();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

终于根据需要插入了所有行...谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多