Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:48+00:00 2026-06-15T15:38:48+00:00

I am currently developing a web application using Struts2 with Spring plugin and hibernate

  • 0

I am currently developing a web application using Struts2 with Spring plugin and hibernate and while I was looking at online examples I saw the use of Service and DAO layers now it came to me what are the real use of Service and data access object layers? If The Service layer is just calling the methods of DAO layers to perform CRUD operations. wouldn’t be sensible to just call the DAO layers methods directly?

Let’s say this example of Dao and Service Layer

PeopleService

  @Transactional
    public class PeopleService {

        private PeopleDao pDao;

        public PeopleDao getPDao() { return pDao; }

        public void setPDao(PeopleDao peopleDao) { this.pDao = peopleDao;   }

        public void createPerson(String name){
            pDao.createPerson(name);
        }

        public List<Person> getPeople(){
            return pDao.getPeople();
        }

    }

PeopleDao

public class PeopleDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    public Person getPersonById(long id) {
        return (Person) sess().load(Person.class, id);
    }

    public void deletePersonById(long id) {
        sess().delete(getPersonById(id));
    }

    public void createPerson(String name) {
        Person p = new Person();
        p.setName(name);
        sess().save(p);
    }

    @SuppressWarnings("unchecked")
    public List<Person> getPeople() {
        return sess().createQuery("from Person").list();
    }

}

My question is what is the real use of Service layers if they are only being injected by their representative DAO and then calling its method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:38:49+00:00Added an answer on June 15, 2026 at 3:38 pm

    It is a good idea to have those two layers when your business logic is more complex than your data logic. The service layer implements the business logic. In most cases, this layer has to perform more operations than just calling a method from a DAO object. And if you’re thinking of making your application bigger, this is probably the best solution.

    Imagine you want to include a City entity and create a relationship between People and City. Here is an example:

    @Transactional
    public class PeopleService {
    
        ....
        private PeopleDAO pDAO;
        private CityDAO cDAO;
    
        ...
    
        public void createPerson(String name, String city)
         throws PeopleServiceException {
            Person p = new Person();
            p.setName(name);
    
            City c = cDAO.getCityByName(city);
            if (c == null) throw new ServiceException(city + " doesn't exist!");
            if (c.isFull()) throw new ServiceException(city + " is full!");
            c.addPeople(p);
    
            sess().save(p);
            sess().save(c);
        }
    
        ...
    }
    

    In this example, you can implement more complex validations, like checking the consistency of the data. And PersonDAO has not been modified.

    Another example:

    DAO and Service layers with Spring

    Definition of Service layer pattern

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developing a Wicket Spring Hibernate application. For development I'm using Jetty as
I'm currently developing a web application using Struts2 framework. This application requires to dynamically
I'm a web developer and now developing a swing application. I'm using Netbeans. Currently
I am trying to use spring security in my application developing restful web services
I've a web application which uses framework like Struts and Hibernate. Currently I'm developing
I am currently developing a Web-Application using Java EE where I'm using a Rich-Javascript-Editor
I am currently developing a medium sized web application using PHP and need to
I'm currently developing a web application that will use Facebook as a authentication service.
I am developing web application using Yii framework. currently i am in the process
I'm currently developing a web application using MYSQL database InnoDB format and need some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.