Calback methods do not work for Hibernate beans

I do everything according to http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html, but neither in-bean methods nor external ones are ever executed. What might be the cause?

@Entity
@EntityListeners(EntityListener.class)
public class User {
    @Id
    @Column
    @GeneratedValue
    private Integer id;

    // etc...

    @PostConstruct
    @PostLoad
    @PostPersist
    @PostUpdate    
    public void magic() {
        System.out.println("YES I AM EXECUTED!");
        System.exit(123);
    }
}

OR

@Entity
@EntityListeners(MyListener.class)
public class User {
    @Id
    @Column
    @GeneratedValue
    private Integer id;

    // etc...       
}

+

public class MyListener {
    @PostPersist
    void postPersist(Object object) {
        System.out.println("CAN'T BELEIVE I SEE THIS!");
        System.exit(234);
    }
}

My code creates, saves and loads beans, but nothing happens on the listeners. This is a piece of the repository thats perform the operations:

@Repository
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public User get(Integer id) {
        return sessionFactory.getCurrentSession().get(User.class, id);
    }

    @Override
    public User save(User user) {
        Session session = sessionFactory.getCurrentSession();

        user = (User) session.merge(user);
        session.saveOrUpdate(user);

        return user;
    }

    // etc...
}

Repository methods are called from services like this one:

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public void something() {
        // just some repo calls + extra logic
    }
}

I do not think I got something special here.

Answers


JPA interceptors mechanism work only when you manipulate entities via JPA EntityManager, they have no effect when you're using Hibernate Session directly.

You'll have to implement Hibernate native interceptors if you want to use the Session API.


Need Your Help

Does open SUSE include NTP server?

linux opensuse

I want to setup a NTP server on open SUSE 11.1.

Checking for new IMAP mail with Java

java imap javamail

I'm trying to implement a small tool using JavaMail to access my IMAP email in specific folders. Then notify if there are new emails in these folders. I'm not necessarily interested in just knowing...