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

  • SEARCH
  • Home
  • 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 88079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:30:12+00:00 2026-05-10T22:30:12+00:00

I’m looking for suggestions on how to inject runtime dependencies into JPA entities retrieved

  • 0

I’m looking for suggestions on how to inject runtime dependencies into JPA entities retrieved from Hibernate. My problem is essentially this:

I have a number of different subclasses of a Transaction object. Each Transaction subclass has different behavior when it is executed, and requires a different set of dependencies from the environment. These Transaction objects are managed as JPA entities by Hibernate, so I can’t effectively use Guice for dependency injection to populate the instances with their environmental dependencies as I do in the rest of my application.

To get around this problem, I’ve taken an approach that’s somewhat akin to the Visitor pattern, as follows:

public abstract class Transaction {     // ...snip...     public abstract void apply(Transactor transactor); }  public class TransactionA extends Transaction {     public void apply(Transactor transactor) {         transactor.execute(this);     } }  public class TransactionB extends Transaction {     public void apply(Transactor transactor) {         transactor.execute(this);     } } // other Transaction subclasses with the same boilerplate  public interface Transactor {     public void execute(TransactionA trans);     public void execute(TransactionB trans);     // corresponding methods for other transaction types. }  public class BeginTransactor {      @Inject      private Foo execAdep;      public void execute(TransactionA trans) {          execAdep.doSomething(...)          }       @Inject      private Bar execBdep;      public void execute(TransactionB trans) {          execBdep.doOther(...)          }  } 

I have various implementations of Transactor for different parts of the transaction lifecycle. These can be dependency-injected using Guice into the context in which I want to process the transactions, where I simply call:

 Transactor transactor = injector.getInstance(BeginTransactor.class); //Guice injection  Transaction t = ... //get a transaction instance  t.apply(transactor); 

What I don’t like about this approach is (1) Not every type of transaction should be executable in each lifecycle phase, but every Transactor must implement an execute() method for every transaction subclass and (2) Essentially none of the injected dependencies are used for processing more than one transaction type.

Essentially, my Transactor interface & implementations have a lot of unrelated crud glopped together. Ideally I’d just have the execute() method on the transaction object itself, but I don’t want the calling code to have to know about the type of the Transaction or the dependencies that it requires. Also, this could make testing harder because I couldn’t easily mock out the execute() method if it were a concrete method on the Transaction object. Using the Transactor interface means that I can easily mock it out as needed.

Can anyone suggest how to address this problem in a typesafe manner that doesn’t result in a bunch of mostly-unrelated behavior being glommed together in the Transactor, but keeps the testability and allows for dependency injection?

  • 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. 2026-05-10T22:30:13+00:00Added an answer on May 10, 2026 at 10:30 pm

    I use guice for transactions, but I use the AOP to carry them out. I have almost no boilerplate, at the expense of a little ‘magic’. As long as your intercepted class is ‘in the club,’ it works really well.

    class BusinessLogic {     @Inject public EntityManager em;      @Transactional     publc void doSomething() {        //...        em.persist(myObj);     }      @Transactional     public void doSomethingElse() {        //...        em.delete(myObj);     } }  class TransactionalInterceptor implements MethodInterceptor {     @Inject static Injector injector;     public Object intercept(MethodInvocation invocation) {         EntityManager em = injector.getInstance(EntityManager.class);         em.getTransaction().begin();         Object result = invocation.proceed();         em.getTransaction().commit();         return result;     } } class TransactionalModule extends AbstractModule {     public void configure() {         requestStaticInjection(TransactionalInterceptor.class);         bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class),                  new TransactionalInterceptor());     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.