I think this could be even more improved, by adding a check whether any NEW entities are present (which would avoid count() thus save sql query).