Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate Throws Found shared references to a collection

I have read thru a number of other questions similar to this one - but being new to Nhibernate, none of them seem to answer my question of why is Inhibernate throwing a "Found shared references to a collection: Order.ShippingAddress.Items" to the following code:

 VendorOrderNotificationAcknowledgement ICheckoutVendorService.SendOrderNotification(VendorOrderNotification request)
{
        OrderRepository repo = new OrderRepository();
        var order =(AbstractOrder) repo.FindByCartId(request.OrderNotification.CartOrderId);

        ShippingAddress billingAddress = order.ShippingAddresses[0];
        var itemsFromDb = billingAddress.Items;
        order.ClearAllShippingAddresses();
        wcfFactory.UpdateOrder(order); //NO ERROR THROWN HERE!
        ShippingAddress shippingAddress = orderHelper.CreateShippingAddress(request.ShippingDetails);
        shippingAddress.Items = itemsFromDb;
        order.AddShippingAddress(shippingAddress);

        order.SourceCode = _sourceCode;
        order.TaxAmount = 0;
        order.GiftCertificateAmount = 0;
        order.Status = StatusCode.Approved;
        order.CreatedAt = request.OrderNotification.OrderTime.Year >2010
            ? request.OrderNotification.OrderTime
            : DateTime.Now;
        order.PurchasedDate= 
                                  request.OrderNotification.OrderTime.Year>2010
            ? request.OrderNotification.OrderTime
            : DateTime.Now;
        order.UpdatedAt = DateTime.Now;

        if (request.OrderNotification.OrderCharges != null)
        {
            order.ShippingAmount = request.OrderNotification.OrderCharges.Shipping;
            order.TaxAmount = request.OrderNotification.OrderCharges.DutyAndTaxes;
        }
        else
        {
            order.ShippingAmount = 0;
            order.TaxAmount = 0;
        }
        order.UseGiftWrap = false;
        order.SourceCode = _sourceCode;
        UpdateEshopWorldOrder(order); // THROWS FOUND SHARED REFERENCES TO A COLLECTION: ORDER.SHIPPINGADDRESS.ITEMS

        var orderDto = orderHelper.CreateOrderDto(billingAddress, orderHelper, order);
        var dtoShippingAddresses = orderHelper.CreateDtoShippingAddresses(order);
        orderDto.ShippingAddresses = dtoShippingAddresses;

        ShippingMethodDto shippingMethodDto = 0;

        var mine = wcfFactory.SendOrder(orderDto);

        //More Code below here ...

}


public OrderDto CreateOrderDto(ShippingAddress billingAddress, OrderHelper orderHelper, AbstractOrder order)
{
    OrderDto orderDto = new OrderDto();
    orderDto.AlternateOrderId = order.Id.ToString();
    orderDto.ConfirmationNumber = order.ConfirmationNumber;
    orderDto.Coupons = new string[0];
    orderDto.DiscountAmount = order.DiscountAmount;
    orderDto.GiftCardAmount = order.GiftCertificateAmount;
    orderDto.PurchaseDate = order.PurchasedDate;
    orderDto.ShippingAmount = order.ShippingAmount;
    orderDto.SourceCode = order.SourceCode;
    orderDto.TaxAmount = order.TaxAmount;
    orderDto.UseGiftWrap = order.UseGiftWrap;
    var customerDto = orderHelper.CreateCustomerDto(billingAddress);
    orderDto.SoldTo = customerDto;
    return orderDto;
}

public void UpdateEshopWorldOrder(AbstractOrder order)
{
    try
    {
        //Session.Update(order);
       // transaction.Commit();
          Session.Flush();
    }
    catch (Exception ex)
    {
       _logger.Debug("order saved failed with an error of " + ex.Message);
       _logger.Error(ex);
       throw;
         }
}

Any insights are appreciated.... thnx

like image 383
user1069733 Avatar asked Oct 11 '12 20:10

user1069733


1 Answers

I think the problem is, that your itemsFromDB collection object is referenced by shippingAddress and also by billingAddress.

Both entities need their own collection-objects. Both collections however may contain references to the same address-objects.

So I assume replacing shippingAddress.Items = itemsFromDb; with something like

shippingAddress.Items.AddRange(itemsFromDb)

or

shippingAddress.Items = new List<ShippingAddress>(itemsFromDb) should do the trick

like image 171
Wolfgang Avatar answered Oct 05 '22 22:10

Wolfgang