I'm working on javaFX project using Hibernate and H2 database,I'm trying follow the concept of MVC design pattern ,so I have created 3 packages :

  1. models :contains POJO classes

    2.1 views.fxml:contains fxml files

    2.2. views.controllers :contains the controllers classes of fxml files

3 . dao:contains classes which they interact with database.

At now every thing is well ,but when i followed a tutoriel ,i found that he added Service layer ,this one has a role to create objects and send them to DAO layer ,i created this example ,but i'm confused if it is true or i'm out the concepts ,this is my example :

Model :

@javax.persistence.Table(name = "ENTREPRENEUR")
public class Entrepreneur implements Serializable {

    private String Id;
    private final StringProperty lastName = new SimpleStringProperty();
    private final StringProperty firstName = new SimpleStringProperty();
    private final StringProperty email = new SimpleStringProperty();
    private final StringProperty password = new SimpleStringProperty();
    private final StringProperty category = new SimpleStringProperty();
    public Date signDate;
    public byte[] picture;

    public Set<Corporation> corporations;

    public Entrepreneur() {
        corporations = new HashSet<>();

    @OneToMany(mappedBy = "entrepreneur")
    public Set<Corporation> getCorporations() {
        return corporations;

    public void setCorporations(Set<Corporation> corporations) {
        this.corporations = corporations;

    @Column(name = "CATEGORY")
    private String getCategory() {
        return category.get();

    public void setCategory(String value) {

    private StringProperty categoryProperty() {
        return category;

    @Column(name = "PICTURE")
    public byte[] getPicture() {
        return picture;

    public void setPicture(byte[] picture) {
        this.picture = picture;

    @Column(name = "SIGN_UP_DATE")
    public Date getSignDate() {
        return signDate;

    public final void setSignDate(Date signDate) {
        this.signDate = signDate;

    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "ID", updatable = false, nullable = false)
    public String getId() {
        return Id;

    public void setId(String Id) {
        this.Id = Id;

    @Column(name = "EMAIL")
    public String getEmail() {
        return email.get();

    public final void setEmail(String value) {

    public StringProperty emailProperty() {
        return email;

    @Column(name = "PASSWORD")
    public String getPassword() {
        return password.get();

    public final void setPassword(String value) {

    public StringProperty passwordProperty() {
        return password;

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName.get();

    public final void setFirstName(String value) {


    public StringProperty firstNameProperty() {
        return firstName;

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName.get();

    public final void setLastName(String value) {

    public StringProperty lastNameProperty() {
        return lastName;

    public void addCompany(Corporation company) {
        if (!corporations.contains(company)) {

Controller :

    private void createAccount() throws IOException {
        String lastNameValue = lastNameField.getText();
        String firstNameValue = firstNameField.getText();
        String categoryValue = this.category.getValue();
        String emailValue = emailField.getText();
        String passwordValue = Security.encrypt(passwordField.getText());
        new EntrepreneurServiceImp().create(lastNameValue, firstNameValue, categoryValue, emailValue, passwordValue);

Service :

public class EntrepreneurServiceImp implements EntrepreneurService {

    public void create(String lastName, String firstName, String category, String email, String password) {

        //Create and fill an object
        Entrepreneur entrepreneur = new Entrepreneur();

        //Send object to DAO layer
        new EntrepreneurDao().save(entrepreneur);




public class EntrepreneurDao extends DAO<Entrepreneur> {

    public boolean save(Entrepreneur entrepreneur) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        return true;

Priviously ,i create and fill the object in controller layer and call the DAO method from the controller ,but now i added Service layer which has a role only with objects ,Is my implementation true or no ?

  • Anything is possible, given enough time, money and programmer effort. – Robert Harvey Mar 19 '18 at 21:14
  • "Is it possible to add Service layer in MVC pattern?". Um, yes. Your service layer is, from the perspective of the controllers, your model. – David Arno Mar 20 '18 at 8:26

Your implementation is right, but there is something you could improve.

If you want to introduce S (= service layer) in MVC, then this is one (usual) way you could do it:

Before: M <--> C <--> V

After: M <--> S <--> C <--> V

The controller uses the service class to do business logic. All the business logic is handled by the service class and is therefore at a centralized place.

The thing you are missing is a Service factory. I would introduce a Service factory that is responsible for intantiating specific Service realizations. In your C (controller) classes, you would want to instantiate the service objects using the service factory. The Service object would then itself instantiate all the DAOs. You then delegate all the CRUD operations to the service class and let it handle.

The advantage of this is that you can then swap the service factory which you pass to your controller classes at a centralized place to completely change the behaviour.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.