Pure methods for updating java objects I've been reading about pure methods and immutability in Java. I have a current application and want to convert all the methods to pure methods. 

My app takes payment from users. I first create a payment in the DB:

Payment payment = new Payment(); 
payment = createPayment(orderId, status, address1, address2, country, state, price);

// createPayment() creates a row in the database table, saving orderId, status, address1, address2, country, state, price
// state is the inital state of the payment, in this cased INITIATED
createPayment() returns a Payment model


// make the payment with the payment processor
PaymentResult paymentResult = paymentService.pay(payment);


PaymentResult contains properties such as authCode, pspReference, resultCode, refusalReason.

The Payment model also contains authCode etc. properties. I now want to update my Payment model, including its status. I currently have:

paymentService.updatePaymentFromPaymentResultOnAuthorise(paymentResult, payment);


And updatePaymentFromPaymentResultOnAuthorise is:

public void updatePaymentFromPaymentResultOnAuthorise(PaymentResult paymentResult, Payment payment) throws PaymentException {

    try {

        boolean success = !isNullOrEmpty(paymentResult.getAuthCode());

        if (success) {
            payment.setLastStatus(payment.getStatus());
            payment.setStatus(PaymentStatus.PAID);
        }

        payment.setPspReference(paymentResult.getPspReference());
        payment.setAuthCode(paymentResult.getAuthCode());
        payment.setResultCode(String.valueOf(paymentResult.getResultCode()));
        payment.setRefusalReason(paymentResult.getRefusalReason());

        paymentDAO.updatePayment(payment);

    } catch (Exception e) {
        throw new PaymentException(String.format("there was an error updating the payment with paymentId %s", payment.getId()));
    }
}


As you can see, updatePaymentFromPaymentResultOnAuthorise is not a pure method as it changes the argument payment. This pattern is repeated throughout the app. 

Is the only way to make this method pure to create a copy of payment within updatePaymentFromPaymentResultOnAuthorise and return this new object? This seems expensive and it will need to be done in many places throughout my app. But is that my only option?
The biggest problem I see is that the objects you pass around in EJB3 are part of the transaction. If you try to reload the payment again from the EntityManager, you will get back the exact same instance you are trying to avoid changing. You will have to jump through hoops in order to reload the payment as a completely new object and that pretty much negates anything that makes pure methods less error prone.

Following best practises is a valid reason but in this case I get the feeling that the baby gets thrown out with the bath water. Don't feel bad, though. EJB3 transaction model is not intuitive at all, needs very good project documentation and guidelines and it still needs constant care taking anyway.