user1883212
user1883212

Reputation: 7859

Persistence model vs. view model

Could someone give an example on where "persistence model" should be separated from "view model"? Why?

Persistence model:

@Entity
public class Employee {

    @Id
    private int id;

    // Some other stuff 

}

View model:

public class EmployeeModel {

    private int id;

    // Some other stuff 

}

And where they should not be separated?

Upvotes: 1

Views: 162

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

Where they should not be separated: when they match exactly, just as in your example.

Where they should be separated: when they don't match. For example, let's say you want to display a table containing, for each row:

  • an employee name
  • the name of his department
  • the number of projects he's involved in

That doesn't match with any persistence model entity, because in the persistence model, you'll have an Employee, with a ManyToOne association with a Department, and a ManyToMany association with Project. To load the data displayed in the table, you'll use an ad-hoc query which will load the required data from these three entities, using joins.

Upvotes: 4

Related Questions