开发者

CDI producer method for data model

开发者 https://www.devze.com 2023-03-22 02:40 出处:网络
I\'d like to be able to @Inject a data model backing a RichFaces 4 ExtendedDataTable, but it requires an EntityManager to do its work. The EntityManager\'s queries need to know the Class, of course, a

I'd like to be able to @Inject a data model backing a RichFaces 4 ExtendedDataTable, but it requires an EntityManager to do its work. The EntityManager's queries need to know the Class, of course, and I'd rather not pass that into method calls (in this case the methods are not called by my code); ideally it would be in the constructor.

Something like this:

public class DataModel<T> {
    @Inject private EntityManager em;
开发者_开发技巧    private Class<T> entityClass;

    public DataModel(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    //Sample method - this class will handle much more complex queries
    public T findEntity(String key) {
        return em.find(entityClass, key);
    }

Is it possible to create a CDI @Producer that can be used to inject this DataModel into my backing beans? I've thought about making a Qualifier so you can do something like

@Inject @JType(value = MyEntity.class) DataModel<MyEntity> dataModel;

But that seemed clumsy, and would also require my @Producer to call new() - which I think would not allow the EntityManager to be injected into the DataModel. Also I'm not sure how you would require the qualifier to be added by the developer.

Or perhaps there's a better way to approach this, and I'm missing something?


I do this using the seam-persistence module from seam3. :

Producer :

public class EntityManagerProducer {

   @Produces
   @ExtensionManaged
   @ConversationScoped
   @PersistenceUnit(unitName = "yourUnitName")
   private EntityManagerFactory emf;
}

Then you can @Inject the entity manager.

Otherwise, there is the DeltaSpike project that seems promising (never used it yet)

0

精彩评论

暂无评论...
验证码 换一张
取 消