0

I am attempting to persist additional entities in a @PostPersit method from a domain class.

As the @PostPersist method is on the domain class I do not have access to any of the service classes/entityManager - to get around this I am using the ApplicationService (singleton) which has the service classes autowired in:

@PostPersist
    public void createNotification() {
        Notification n = new Notification();
        n.setActivity(this);
        ApplicationService.getInstance().sendNotifications(n);
    }

The ApplicationService method just invokes a method in the Autowired service class:

public void sendNotifications(Notification n) {
        notificationService.distributeNotifications(n);
    }

The service class is a standard spring annotated service class and has a transactional method that creates and persists new objects

@Transactional
public void distributeNotifications(Long accountId, Notification n) {
    this.createNotification(n);
    ...
}

However, the additional entity is never being persisted - Can anyone advise where the above is going wrong? Am I mis-understanding the transactional boundaries in hibernate?

Thanks.

rhinds
  • 9,976
  • 13
  • 68
  • 111

1 Answers1

0

The reason probably lies in the fact that you create the singleton yourself? Shouldn't it be spring creating the instance so that it interprets the @Transactional annotation?

  • Apologies, that was a bit misleading - its still a spring managed class and implements Springs ApplicationContextsAware interface (described more or less in the top two ansers here: http://stackoverflow.com/questions/129207/getting-spring-application-context) the getInstance() method uses the static app context to get the spring bean – rhinds Nov 30 '11 at 09:11