Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1070003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:30:06+00:00 2026-05-16T20:30:06+00:00

My problem is very similar to this one : Injecting fields via Spring into

  • 0

My problem is very similar to this one : Injecting fields via Spring into entities loaded by Hibernate

The difference is that , I am using JPA2 entities , not hibernate . While the underlayer is still hibernate (3.5.5).

My spring version is 3.0.4.

What’s the corresponding eventListeners in JPA’s world ?

Sample code from the original post :

class Student {
   int id; //loaded from DB
   String name; //loaded from DB
   int injectedProperty; //Inject via Spring
   transient Service serviceImpl; //Inject via Spring
}

I know there may be aspectJ’s solutions , but I’d rather like a pure-java solution.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:30:07+00:00Added an answer on May 16, 2026 at 8:30 pm

    What’s the corresponding eventListeners in JPA’s world ?

    JPA has Entity Listeners (and Callback Methods). From the JPA 2.0 specification:

    3.5 Entity Listeners and Callback Methods

    A method may be designated as a
    lifecycle callback method to receive
    notification of entity lifecycle
    events. A lifecycle callback method
    can be defined on an entity class, a
    mapped superclass, or an entity
    listener class associated with an
    entity or mapped superclass. An entity
    listener class is a class whose
    methods are invoked in response to
    lifecycle events on an entity. Any
    number of entity listener classes can
    be defined for an entity class or
    mapped superclass.

    (…)

    The entity listener class must have a
    public no-arg constructor.

    Entity listeners are stateless. The
    lifecycle of an entity listener is
    unspecified.

    The following rules apply to lifecycle
    callbacks:

    • Lifecycle callback methods may throw unchecked/runtime exceptions. A
      runtime exception thrown by a callback
      method that executes within a
      transaction causes that transaction to
      be marked for rollback.
    • Lifecycle callbacks can invoke JNDI, JDBC, JMS, and enterprise beans.
    • In general, the lifecycle method of a portable application should not
      invoke EntityMan- ager or Query
      operations, access other entity
      instances, or modify relationships
      within the same persistence context. A
      lifecycle callback method may modify
      the non-relationship state of the
      entity on which it is invoked.

    (…)

    3.5.1 Lifecycle Callback Methods

    Entity lifecycle callback methods can
    be defined on an entity listener class
    and/or directly on an entity class or
    mapped superclass.

    Lifecycle callback methods are
    annotated with annotations designating
    the callback events for which they are
    invoked or are mapped to the callback
    event using the XML descriptor.

    The annotations used for callback
    methods on the entity class or mapped
    superclass and for callback methods on
    the entity listener class are the
    same. The signatures of individual
    methods, however, differ.

    Callback methods defined on an entity
    class or mapped superclass have the
    following signature:

    void <METHOD>()
    

    Callback methods defined on an entity
    listener class have the following
    signature:

    void <METHOD>(Object)
    

    The Object argument is the entity
    instance for which the callback method
    is invoked. It may be declared as the
    actual entity type.

    The callback methods can have public, private, protected, or package level access, but must not be static or final.

    (…)

    Here is an example (from the spec):

    @Entity
    @EntityListeners(com.acme.AlertMonitor.class)
        public class Account {
        Long accountId;
        Integer balance;
        boolean preferred;
        @Id
        public Long getAccountId() { ... }
        ...
        public Integer getBalance() { ... }
        ...
        @Transient // because status depends upon non-persistent context
        public boolean isPreferred() { ... }
        ...
        public void deposit(Integer amount) { ... }
        public Integer withdraw(Integer amount) throws NSFException {... }
    
        @PrePersist
        protected void validateCreate() {
            if (getBalance() < MIN_REQUIRED_BALANCE)
                throw new AccountException("Insufficient balance to open an account");
        }
    
        @PostLoad
        protected void adjustPreferredStatus() {
            preferred = (getBalance() >= AccountManager.getPreferredStatusLevel());
        }
    }
    
    public class AlertMonitor {
        @PostPersist
        public void newAccountAlert(Account acct) {
            Alerts.sendMarketingInfo(acct.getAccountId(), acct.getBalance());
        }
    }
    

    In your case, you’ll probably want PostLoad callback methods.

    See also the Chapter 6. Entity listeners and Callback methods in the Hibernate Entity Manager documentation.

    But to put it simply, what you want to do is not that simple with JPA and using AspectJ class weaving with the @Configurable annotation might be the best option.

    References

    • JPA 2.0 Specification
      • Section 3.5 “Entity Listeners and Callback Methods”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current problem is very similar to this one . I have a downloadFile(URL)
My problem is very similar to this question , however I am posting a
I have a problem very similar to the one described here: https://microsoft.public.win32.programmer.kernel.narkive.com/Ly2P8Yp2/prevent-vista-from-marking-my-application-as-non-responding That thread
I have an error very similar to the one addressed in this question .
My problem is very similar to eight queens puzzle. I've got 2-dimensional array (N
We have very strange problem, one of our applications is continually querying server by
Very odd problem as this is working perfectly on our old Classic ASP site.
i have very simple problem. I need to create model, that represent element of
A very niche problem: I sometimes (30% of the time) get an 'undefined handler'
I have a very strange problem. Under some elusive circumstances I fail to apply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.