Entity factory

#1

Hi,

is it possible to create Entities with a custom factory? I want to create Entity instances via dependency injection like:

@Entiy
public class User {
    private final UserService userService;
    
    @Inject
    private User(UserService userService) {
        this.userService = userService;        
        ...
   }
}

public interface EntityFactory {
    <T> T create(Class<T> entityClass);   
}

EntityManagerFactory entityManagerFactory = (...);
entityManagerFactory.setEntityFactory(myFactory);

The JPA listeners are unsufficient since they are also created without the possibility to inject dependencies (at least without using static variables which are very bad code style). An alternative would be if one can create JPA listener instances inside his application and add this listener via API call to ObjectDB (like .

Is it possible to implement such a functionality? I think this would make a lot of people happier since Hibernate also offers such features.

Thanks and best wishes

Markus

#2

Only standard Java EE / Spring Framework injection is supported.

If it may help, you can obtain the EntityManager in a JPA listener method by using JDO:

    EntityManager em = // find the EntityManager that manages this
            (EntityManager)JDOHelper.getPersistenceManager(this);
ObjectDB Support
#3

Thanks, I managed to use my own dependency injection method:

I store the current Context in a ThreadLocal and access it inside the new Entity to inject the dependencies.

Reply