@Test
  public void testManualOrderEntryPositions() {
    final OrderModel newOne = createNewOrder();

    // take out entries
    final List<AbstractOrderEntryModel> entries = newOne.getEntries();
    newOne.setEntries(null);

    modelService.save(newOne);

    Assert.assertFalse(modelService.isNew(newOne));
    Assert.assertEquals(Collections.EMPTY_LIST, newOne.getEntries());

    Assert.assertTrue(modelService.isNew(entries.get(0)));
    Assert.assertTrue(modelService.isNew(entries.get(1)));
    Assert.assertTrue(modelService.isNew(entries.get(2)));
    Assert.assertTrue(modelService.isNew(entries.get(3)));

    // now save in reverse order
    AbstractOrderEntryModel entry = entries.get(3);
    modelService.save(entry);
    Assert.assertEquals(Integer.valueOf(3), entry.getEntryNumber());

    entry = entries.get(2);
    modelService.save(entry);
    Assert.assertEquals(Integer.valueOf(2), entry.getEntryNumber());

    entry = entries.get(1);
    modelService.save(entry);
    Assert.assertEquals(Integer.valueOf(1), entry.getEntryNumber());

    entry = entries.get(0);
    modelService.save(entry);
    Assert.assertEquals(Integer.valueOf(0), entry.getEntryNumber());

    modelService.refresh(newOne);

    Assert.assertFalse(modelService.isNew(newOne));
    Assert.assertFalse(modelService.isNew(entries.get(0)));
    Assert.assertFalse(modelService.isNew(entries.get(1)));
    Assert.assertFalse(modelService.isNew(entries.get(2)));
    Assert.assertFalse(modelService.isNew(entries.get(3)));

    assertEntriesOrder(newOne);
  }
  private OrderModel createNewOrder() {
    final OrderModel order = modelService.create(OrderModel.class);

    final UserModel user = userService.getCurrentUser();

    order.setUser(user);

    order.setCurrency(commonI18NService.getBaseCurrency());
    order.setDate(new java.util.Date());
    order.setNet(Boolean.FALSE);

    final OrderEntryModel entry1 = modelService.create(OrderEntryModel.class);
    entry1.setOrder(order);
    entry1.setQuantity(Long.valueOf(1));
    entry1.setUnit(productService.getOrderableUnit(product1));
    entry1.setProduct(product1);
    entry1.setEntryNumber(Integer.valueOf(0));

    final OrderEntryModel entry2 = modelService.create(OrderEntryModel.class);
    entry2.setOrder(order);
    entry2.setQuantity(Long.valueOf(1));
    entry2.setUnit(productService.getOrderableUnit(product2));
    entry2.setProduct(product2);
    entry2.setEntryNumber(Integer.valueOf(1));

    final OrderEntryModel entry3 = modelService.create(OrderEntryModel.class);
    entry3.setOrder(order);
    entry3.setQuantity(Long.valueOf(1));
    entry3.setUnit(productService.getOrderableUnit(product3));
    entry3.setProduct(product3);
    entry3.setEntryNumber(Integer.valueOf(2));

    final OrderEntryModel entry4 = modelService.create(OrderEntryModel.class);
    entry4.setOrder(order);
    entry4.setQuantity(Long.valueOf(1));
    entry4.setUnit(productService.getOrderableUnit(product4));
    entry4.setProduct(product4);
    entry4.setEntryNumber(Integer.valueOf(3));

    order.setEntries((List) Arrays.asList(entry1, entry2, entry3, entry4));

    return order;
  }