Fix detached entity exception