Jukka Palomäki opened SPR-5999 and commented

Given the rich Entity model promoted by e.g. Spring Roo, it would be nice if Spring core would support @Transactional static methods via AspectJ. Have a look at the sample below.

@Entity
class Reservation {

    @Transactional
    public void save() {
        // ...
    }

    @Transactional
    public void delete() {
        // ...
    }

    //...

    @Transactional
    public static void deleteAll() {
         // ...
    }
}

Currently this sort of design can work out for example by creating a (private) @Transactional instance method (e.g. doDeleteAll()), and delegating to it from within the static method. Though fairly concise, this approach feels sub-optimal. Another approach is to create a @Service bean to encapsulate this behavior, but for such a simple operation, it seems like overkill.


Affects: 3.0 M3

Reference URL: http://forum.springsource.org/showthread.php?t=76011

6 votes, 8 watchers

Comment From: spring-projects-issues

Alejandro @ Arson Technology And Innovation Company commented

I have the very same problem here, trying to implemente a deleteAll() method.

I'll try the approach suggested by Jukka.

Comment From: spring-projects-issues

Jukka Palomäki commented

Here's another approach:

public static void deleteAll() {
    class Delegate {
        @Transactional
        void doDeleteAll() {
            // ...
        } 
    }
    new Delegate().doDeleteAll();
}

Comment From: sbrannen

Closing outdated, unresolved issues. Please, reopen if still relevant.