Announcement Announcement Module
No announcement yet.
AOP and Hibernate value objects Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP and Hibernate value objects

    I am using Hibernate (and Spring) in my application and I have a number of POJOs used for all the tables I'm accessing. I call them Value Objects, so for example I have UserVO and CustomerVO. Each of these VO's have a number of String fields in them.

    I want to use a simple BeforeMethodAdvice that does something like:

    public void before(Method m, Object[] args, Object target) throws Throwable {
           if(args[0] instanceof String) {
               String buffer = (String)args[0];
               buffer = StringUtils.trimToEmpty(buffer);
    I want this to advise all of the set methods on the VO objects. This seems easy since all the objects are named *VO and all the set methods follow javabeans style. Also, all the VO's are in their own package: db.beans

    I was planning on using a JdkRegexpMethodPointcut with a pattern like:

    My problem is in the proxy configuration. I want to be able to apply this advice to all the beans in the db.beans pacakge, but these beans are created by hibernate and not my application or spring so I can't figure out how to proxy them without explicitly tying hibernate to the hard coded proxies. This solution seems to go against what I'm trying to use AOP in the first place for...

    Any help anyone could provide would be really appreciated. I'm new to AOP and kind of stuck here.


  • #2
    Hibernate has this kind of functionality built in through the EntityInterceptor interface. Personally, I wouldn't use a proxy-based approach for something this fine-grained - instead I would look at using AspectJ to weave in the additional code needed.



    • #3
      Wow.. thanks Rob.. It's kind of interesting since I got the idea last night after finishing chapter 6 and 7 of your book where you say that I should identify crosscutting instances and implement them with AOP.

      I came to the same conclusion on using AspectJ. I haven't looked at it, but I'm aware that it would force me to use a non java compiler correct? That may complicate my build and depolyment, but I'll play with it..

      semi off topic : thanks for a great book


      • #4
        You can use load time weaving with AspectJ. This way you can compile your normal code with Java, your aspects with ajc and then use load time weaving.

        Alternatively, look at AspectWerkz which has a load weaver but uses pure Java aspects. It is probably more suited to your use case than Spring. This feature of AW will hopefully make it into AJ soon.



        • #5
          update on this

          I have the same question as the original poster and am wondering where we're at now. I'd rather not have to configure aspectj, but I'd like to do aop against domain objects (not managed by spring). I know spring uses aop to manage the transactions - does it by chance also manage the getters and setters before and after queries?


          • #6
            AspectJ5 now supports load-time weaver (similar to what robh mentions).

            As I was reading this thread, I wondered why Rob hopes load-time weaver will make into AspectJ; it already has made it in. Then I saw the date of the post -- March 2005 :-).