The representer theorem for Hilbert spaces: a necessary and sufficient condition