Fixed query problem when a referenced entity has been transacted earlier.