Are Hibernate Entities the same as the domain models?
See the following example.
Method 1 - Domain model and Entity are same class. Domain model "is-an" entity
@Entity
@Table(name = "agent")
class Agent
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "agent_number", unique = true, nullable = false)
private String agentNumber;
@Column(name = "agent_name", nullable = false)
private String agentName;
// Busines logic methods
}
Method 2 - Domain and Entity are different functions. Domain model "has-an" entity
class Agent
{
// Hibernate entity for this domain model
private AgentEntity agentEntity;
// Getters and setters to set the agentEntity attributes
// Business logic
}
From the above 2 methods, which of them are the correct way to implement DDD? I believe method 2 is the right way because you are essentially controlling the access to a sensitive object and the enclosing object (Domain model) has all the business logic/operations on the domain model.
But my workplace colleagues suggests that they are essentially the same. And according to them the purpose of Hibernate Entity is to represent the domain model in a given system. Modelling the entity as the domain model actually makes the design simpler. This is because the repository takes an Entity to execute CRUD operations.
So if model "has-an" entity, then the repository must be dependency injected into the domain model to save the entity. This will make the design unnecessarily complicated.