Announcement Announcement Module
Collapse
No announcement yet.
Don't repeat the dao - LazyInitializationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    New question related to this topic:

    With this configuration, how would I extend the GenericService?

    my idea is to create a PartnerService, which defines a specific method related to partners but is still capable of offering all the methods in GenericService...
    once this is done, it is also logical to extend the GenericDAO (as in PartnerDAO).

    the PartnerDAO would execute special partner-specific queries in the DB and would be used by PartnerService.

    Can you follow me?
    I've been trying for a while now, but I cannot make it work.
    can you help me?

    Comment


    • #17
      Sure I can help!

      This is a good question. I do this all the time. Here is an example from my code:

      PersonService.java:
      Code:
      public interface PersonService extends GenericService<Person, Long>
      {
        public Person loadByPersonId(Long personId);
        public Person loadByUsername(String username);
        public Person loadByEmail(String email);
        public void recoverPassword(String email);
        public String assignSystemPassword(Person person);
        public void updatePassword(Person person, String password);
      }
      PersonServiceImpl.java:
      Code:
      public class PersonServiceImpl extends GenericServiceImpl<Person, Long> implements PersonService
      {
        private PersonDao personDao;
      
        public PersonServiceImpl()
        {
          super(Person.class);
        }
      
        @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
        public Person loadByPersonId(Long personId)
        {
          return personDao.load(personId);
        }
      
        @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
        public Person loadByUsername(String username)
        {
          return personDao.loadByUsername(username);
        }
      
        @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
        public Person loadByEmail(String email)
        {
          return personDao.loadByEmail(email);
        }
      
        @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
        public void recoverPassword(String emailAddress)
        {
          Person person = personDao.loadByEmail(emailAddress);
          if(person != null)
          {
            String password = PersonUtil.generateRandomPassword();
            person.setForceChangePassword(true);
            updatePassword(person, password);
      
            EmailUtils.sendResetPasswordEmail(person);
          }
        }
      
        @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
        public String assignSystemPassword(Person person)
        {
          String password = PersonUtil.generateRandomPassword();
          person.setForceChangePassword(true);
          updatePassword(person, password);
          return password;
        }
      
        @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
        public void updatePassword(Person person, String password)
        {
          if(person == null || StringUtils.isBlank(password))
            return;
          person.setPassword(SecurityUtils.encrypt(password));
          personDao.insertOrUpdate(person);
        }
      
        public void setPersonDao(PersonDao personDao)
        {
          this.personDao = personDao;
        }
      }
      I hope this example of how to extend the GenericService is helpful. Also, this shows how to use a Dao that extends the GenericDao.

      NOTE: I've found that extending the GenericService or GenericDao is nice and easy. However, the IBM article I read that showed me how to do this explains that you can use springs AOP to get the same effect, but you don't need to extend the GenericService or GenericDao. I recommend reading the article. http://www.ibm.com/developerworks/ja...enericdao.html

      Comment

      Working...
      X