To further clarify my initial problem, I rewrote the question with more \'DDD\'-termini, common patterns and discussion arguments. The orginal version can be
I would place it in a factory. Generating id shouldn't be a part of domain logic in my opinion, because it's really an infrastructure matter. You can take id from DB or generate it with uuid or whatever. It's a detail. Also remember that only interface of a factory belongs to domain layer, not its implementation.
About your doubts for factory, if you use factory to create entities then you should use it everywhere. This is how I do it.
Vaughn Vernon author of Implementing Domain Driven Design advocates creating unique ids in repositories like this:
public TenantId nextIdentity() {
return new TenantId(UUID.randomUUID().toString().toUpperCase());
}
TenantId
is a value object that wraps the identity of the Entity.