3

I know that spring roo can generate the entity,and controller.But my requirement is to generate the DAO and Service layer or atleast DAO. Is there any way to generate Entity,Controller and DAO using spring-roo

rakesh menon
  • 1,081
  • 3
  • 15
  • 30

2 Answers2

6

I thought that there were no way to make DAOs but I stand corrected.

In the recently released Roo 1.2 there is now the concept of JPA Repositories and a new Service element as detailed in the Spring blog.

madth3
  • 7,275
  • 12
  • 50
  • 74
  • Thanks for your help.Now am able to create services and repositories.I guess this Repositories can be used instead of DAO am i right? – rakesh menon Feb 24 '12 at 01:42
  • 1
    Yes. Theoretically these concepts are not the same but in implementation they are frequently confused. http://stackoverflow.com/questions/8550124/what-is-the-difference-between-dao-and-repository-patterns. Also, from an architectural point of view they would ocuppy the same space being used by the controllers or the services. – madth3 Feb 24 '12 at 01:52
  • Spring blog url is broken. New [link](http://spring.io/blog/2011/09/14/new-application-layering-and-persistence-choices-in-spring-roo-1-2) – bioinfornatics Jan 10 '14 at 14:15
1

For an example you have a User class with fields username, password and you need to create abstraction layers

Entity -> Repository (DAO) -> Service

as

User-> UserRepository -> UserService

There are basically 2 ways of doing things - first one with JPA Repository and second one with Mongo Repository, apart from default ActiveRecord style. Setting up with Mongo or JPA is similar. I am explaining commands for JPA Repository here.

1) execute setup command

jpa setup --provider HIBERNATE --database HYPERSONIC_PERSISTENT

2) define new User entity setting default activeRecord to false (important)

entity --class ~.domain.User --activeRecord false

3) define fields for this User entity

field string --fieldName userName --notNull --sizeMin 3 --class ~.domain.User
field string --fieldName password --notNull --sizeMin 3 --class ~.domain.User

4) Create a new JPA repository interface using repository jpa command, which is equivalent to creating new repository interface by extending the spring data JpaRepository class public interface UserRepository extends JpaRepository<User, Long> {/*Code*/}. This provides all the CRUD functionalities and you don't need to add anything. You can add other searching functionalities. Repository interface is similar to DAO interface. DAO is more tightly coupled with the persistence entities while Repository is more related to the domain objects.

repository jpa --interface ~.repository.UserRepository --entity ~.domain.User

5) Now add Service layer, where you can add all the business logic to your application. This step will create UserService interface and UserServiceImpl classes

service --interface ~.service.UserService --entity ~.domain.User

The layering setup is finished. You can now create web layer and execute your code.

web mvc setup
web mvc all --package ~.web
Prabhu
  • 5,296
  • 4
  • 37
  • 45