Announcement Announcement Module
Collapse
No announcement yet.
Using Spring Roo with DTO, service and repository patterns Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Spring Roo with DTO, service and repository patterns

    Hi,
    I am new to Spring Roo, so please excuse me if my questions are little naive.
    I tried to create a simple application containing domain objects and REST interface (similar to the PizzaShop sample). It worked great, without any problems.
    Now I wanted to re-create the same application using DTO, service and repository patterns. Following is what I am trying to achieve:-
    1) Repository will work on the domain objects and handle all the database related stuff.
    2) Services will enclose the business logic and use the repository to access the domain models. Services will convert the domain model to DTO object.
    3) Controller will access the services and will operate with the JSON for DTO objects and provide REST interface.

    So I took the following approach:-
    1) Generated the domain objects using "entity jpa" command. Added the attributes to the domain objects.
    2) Generated the repository for the domain objects using "repository jpa --interface" for the domain objects.
    3) I created the DTO objects using "entity jpa" commands. I manually removed the @RooJpaEntity annotations to prevent the database tables getting created for these objects. I could have achieved this using the "class" command, but couldn't figure out a way to generate the *_Roo_JavaBean.aj file.
    4) Added json to the DTO objects using "json add " command.
    5) I created the service layer using "service --interface" for the DTO objects. This created the *_Service_Roo_Service.aj file with unimplemented methods, which is fair enough since there is no repository nor any JPA entity associated with DTO object. So, I will have to implement these methods manually.
    6) Generated the controllers for the DTO objects using "web mvc json" command.

    Following are the problems faced by me:-
    1) I would like to know if the approach taken to use Roo with DTO, service and repository pattern is correct or not? Is there any better way to achieve this?
    2) The "web mvc json" command doesn't generate the *_Roo_Controller_Json.aj files. It just generates the empty controller.java file. It seems those files are generated only when controller is generated for objects with @RooJpaEntity? Am I correct? If yes, then is there any way to generate those files in this scenario?
    3) I manually added the code which is generally present in the *_Roo_Controller_Json.aj file to one of the controller.java file and implemented one of the methods in the corresponding service object. I tested these methods using curl and I got the expected output. I tested all this while the roo shell was running. But now whenever I restart the roo shell, it deletes all *_Service_Roo_Service.aj and in the controller I get an exception saying "Injection of autowired dependencies failed", in which it is saying that it is unable to find a matching bean for the service object.
    I didn't understand why the *_Service_Roo_Service.aj files are deleted on startup of roo shell and why do I get this exception upon deletion of these files?

    Thanks in advance and apologies for such a lengthy post.
    Last edited by jayendrap; Jan 25th, 2012, 06:59 AM.

  • #2
    Instead of just describing the approach you used and what you did, may I suggest you post a roo script with all the relevant commands, so we can quickly see not only what you did but also recreate the project if needed?

    Inside your project there's a "log.roo" file where all your roo commands have been saved and some log opened / closed time stamps too. Open this in an editor and resave it under another name, keep the file extension. Then trim away errors and the opened / closed timestamps and post this here in a form that will create a new project when run as a roo script. Test it before you post.

    From your description it's clear you are using Roo 1.2Release or later, but I'm not clear whether you created your entities with the "--activeRecord false" option, which should be used if you're going to use Daos. If you choose that you are building a traditional Spring application, not specific to Roo in its basic pattern. Active Record is the default pattern used by Roo.

    I don't understand what you expected from the second 3 at all. Please be more descriptive on this.

    On the third 3, the files are being deleted since Roo notices you have implemented these yourself. This would have been immediately obvious to you if you had used refactor "push-in", instead of copying code from the itd file.

    Did you read the documentation inside "docs"? If not, read it all.
    Last edited by MiB; Jan 25th, 2012, 07:46 AM.

    Comment


    • #3
      Thanks for your reply,

      Originally posted by MiB View Post
      Instead of just describing the approach you used and what you did, may I suggest you post a roo script with all the relevant commands, so we can quickly see not only what you did but also recreate the project if needed?

      Inside your project there's a "log.roo" file where all your roo commands have been saved and some log opened / closed time stamps too. Open this in an editor and resave it under another name, keep the file extension. Then trim away errors and the opened / closed timestamps and post this here in a form that will create a new project when run as a roo script. Test it before you post.
      Got your point, I will update the file soon.

      Originally posted by MiB View Post
      From your description it's clear you are using Roo 1.2Release or later, but I'm not clear whether you created your entities with the "--activeRecord false" option, which should be used if you're going to use Daos. If you choose that you are building a traditional Spring application, not specific to Roo in its basic pattern. Active Record is the default pattern used by Roo.
      Yes, I am using Roo 1.2 and i created my entities using "--activeRecord false". It will be much more clear to you once I update the roo script file.

      Originally posted by MiB View Post
      I don't understand what you expected from the second 3 at all. Please be more descriptive on this.
      I generated the DTO objects using "entity jpa --activeRecord false" command and then removed the @RooJpaEntity annotations in the generated .java files. I did this because I could not find any command to create the *_Roo_JavaBean.aj file. I wanted to use the mutators and accessors generated by Roo.
      I also tried to generate the DTO objects using the "class" command, added json to these classes using "json add" command. But still, whenever I try to generate the controllers using "web mvc json", the *_Roo_Controller_Json.aj file is not generated.

      Originally posted by MiB View Post
      On the third 3, the files are being deleted since Roo notices you have implemented these yourself. This would have been immediately obvious to you if you had used refactor "push-in", instead of copying code from the itd file.
      I understand the part where Roo deletes the file when I implement all the methods from the *.aj file. Here, it is deleting the file even when I have implemented just one method. Also, I implemented the method in only one of the controller and service, but Roo deletes all the *_Service_Roo_Service.aj files.

      Comment


      • #4
        Originally posted by jayendrap View Post
        I generated the DTO objects using "entity jpa --activeRecord false" command and then removed the @RooJpaEntity annotations in the generated .java files.
        This is incorrect I'm pretty sure. Read up in the blog entry NEW APPLICATION LAYERING AND PERSISTENCE CHOICES IN SPRING ROO 1.2 under "JPA Repository" for a better approach. I have not seen the need of a repository yet, so I suggest any reader that have experience/knowledge of this chime in here.
        Originally posted by jayendrap View Post
        I did this because I could not find any command to create the *_Roo_JavaBean.aj file. I wanted to use the mutators and accessors generated by Roo.
        Just use/keep @RooJavaBean in your entity.

        Originally posted by jayendrap View Post
        I understand the part where Roo deletes the file when I implement all the methods from the *.aj file. Here, it is deleting the file even when I have implemented just one method.
        Maybe this was the only method left?

        Originally posted by jayendrap View Post
        Also, I implemented the method in only one of the controller and service, but Roo deletes all the *_Service_Roo_Service.aj files.
        This sounds odd. Sometimes Roo does some strange thing like this. Most likely because we have confused it with strange code. :-)

        Comment


        • #5
          In the Roo manual I suppose the chapter "10.2.2. JPA Repository" is mandatory reading for anyone wanting a repository / DAO layer.

          Comment


          • #6
            Thanks once again for your reply.
            Yes, you are correct that I tried my best to confuse Roo, with a strange code.
            So, now I am thinking of scrapping whatever I was doing earlier and use the following approach:-
            1) Generate all the layers, i.e repository, service and controller for the domain objects (similar to the way shown in the PizzaShop example).
            2) Generate DTO objects using "class --class" command.
            3) As you had suggested, in Eclipse use refactor > push-in option to move all the code from *.aj files to .java file and modify it for using the DTO objects wherever necessary.

            I am attaching my roo script file for your reference. (If necessary, remove the .txt extension before you use it).
            I just want to know if this approach is correct or not? Is there any better way in which I can do this?

            Comment


            • #7
              Unfortunately I cannot look at your script before I've installed jdk 6 on my new machine and I'm still waiting for my OS to arrive and it will be another week or so. Hopefully someone else will try it before me. Let us know how things proceed your end.

              Comment

              Working...
              X