Question

I'm migrating a big application to CDI with JPA. Before I can change all code, I need to know if following pattern will work. This will allow me to migrate the application partially.

My Managed Bean:

@Named
@Stateless
public class LanguageBoundary implements Serializable {

    @Inject
    LanguageService languageService;

    @Inject
    CrudService crudService;

    public LanguageEntity findById(Long id) {
        return crudService.find(LanguageEntity.class, id);
    }

...

Can I use in any other class:

// Methods: DAO - Used by original deprecated parent wrapper class
public LanguageEntity findByIdImpl(Id id) {

    LanguageBoundary lb = new LanguageBoundary();
    return lb.findById(id.getValue());
}

Thanks for the advise.

Was it helpful?

Solution 2

To do something like this, you need to get a reference to the object. You can do this using CDI. In CDI 1.1 you can get the CDI utility class. In CDI 1.0 you can get a reference to the bean manager using JNDI to do the equivalent. Look at this code for reference: https://github.com/apache/deltaspike/blob/master/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java#L98

I'm curious why you're using @Named. Are you planning to reference the EJB directly in your UI?

OTHER TIPS

If you instantiate it, the bean wouldn´t be managed by the container, so it´s probably not what you want. Instead, you should inject it with @Inject

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top