The baryon fraction in hydrodynamical simulations of galaxy clusters