lazy initialization of CDI injected bean in setter - cdi

So I have a bean which contains a category field. If that category field is set to a specific value, for instance "MATCH" I want to initialize a bean. However doing it in the setter of the category is kind of an anti pattern and I'd like to know if in this case that could be considered alright or maybe there is a better way to do it.
I don't want to use postConstruct because the bean is used a really low percentage of the time. So having DB calls in PostConstruct is non sens in my opinion in this case.
#Named
#ViewScoped
public class BeanA implements Serializable {
private Category category;
#Inject
private MatchCreation matchCreation;
public void setCategory(Category category) {
this.category = category;
if("MATCH".equals(category.getName()){
matchCreation.init(); // I'll put a check here to not initialize it twice
}
}
}
#Named
#ViewScoped
public class MatchCreation {
private List<Team> teamList;
private List<Map> mapList;
#EJB
private TeamService ts;
#EJB
private MapService ms;
public void init() {
teamList = ts.getProTeams();
setMapList(ms.getAllMaps());
}
}
Also in that particular bean I have 9 injections. I don't have to be afraid to use those right, I don't have to try to have the bare minimum ?

Related

Passing value between JSF Forms. I can't get it to work

I read the other posts about this subject but I still can't get it to work
This are my beans:
Bean1:
#ManagedBean()
#SessionScoped
public class Bean1 implements Serializable {
//Here are some important Properties
public String buttonPressed() {
return "bean2.xhtml";
}
}
<h:form>
<p:commandButton action="#{Bean1.buttonPressed}" value="Do Work"/>
</h:form>
Bean2:
#ManagedBean()
#SessionScoped
public class Bean2 implements Serializable {
#ManagedProperty(value = "#{Bean1}")
private Bean1 b1;
//getter/setter is here
public String doWorkOnSubmit() {
//Access important Properties from bean1
b1.getFoo()
}
}
Now I have two Problems
1.) How to call "doWorkOnSubmit" if the button in Bean1 is pressed? I can't use the constructor because it's SessionScoped and I don't know how to call doWorkOnSubmit un submit
2.)The managed property "b1" is sometimes null
Since on clicking of Do Work button you are calling Bean1.buttonPressed() action method you can call Bean2s doWorkOnSubmit() by injecting Bean2 in Bean1.
Here is your code altered:
#ManagedBean()
#SessionScoped
public class Bean1 implements Serializable {
//Here are some important Properties
/*
* Inject Bean2 here. since both beans are session scoped,
* there ain't gonna be any problem in injection.
*/
#ManagedProperty(value = "#{Bean2}")
private Bean2 b2;
//GETTER SETTER for b2
public String buttonPressed() {
//Here you will be invoking injected managed beans method.
b2.doWorkOnSubmit();
return "bean2.xhtml";
}
}

NullPointerException when trying to access JPA service class

I've the below managed bean
#ManagedBean
#RequestScoped
public class customerBean {
private Customer customer;
private CustomerJpaController customerJpa;
#PostConstruct
public void init() {
customer = new Customer();
}
public String addCustomer() throws Exception {
customerJpa.create(customer);
return "customer";
}
// getter and setter
}
The CustomerJpaController looks like below:
public class CustomerJpaController implements Serializable {
#PersistenceContext(unitName = "JFSCustomerPU")
private EntityManagerFactory emf = null;
private UserTransaction utx = null;
public CustomerJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
// ...
}
When addCustomer() is invoked from the view, it throws java.lang.NullPointerException at line customerJpa.create(customer);. How is this caused and how can I solve it?
In your code sample, your CustomerJpaController is never instantiated. So, you get a null pointer exception.
I advise you to switch to CDI and rely on its injection method to have your entity manager (factory?) properly instantiated and injected in your controller when this last is instantiated. And, so, to use #Named instead of #ManagedBean.
So, you would have :
#Named
#RequestScoped
public class CustomerJpaController implements Serializable {
...
}
(or whichever scope better fits your need)
It seems to me that you should use an EntityManager (EM) rather than an EntityManagerFactory (EMF) in your controller.
If your EMF is container managed and you have only one persistence unit, you can use the standard JPA #PersistenceContext annotation :
#PersistenceContext
private EntityManager entityManager;
If your EMF is not managed, you can leverage the power of deltaspike JPA module (remember : deltaspike is good for you :-) ) and inject an EntityManager in your controller :
#Named
#RequestScoped
public class CustomerJpaController implements Serializable {
#Inject
private EntityManager em;
}
This requires the implementation of an EntityManagerProducer class, which can have any name but must have one method annotated #Produces #RequestScoped returning an EntityManager and another one taking an EntityManager parameter annotated with #Disposes. Ex :
public class MyEntityManagerProducer {
#Inject
#PersistenceUnitName("myPU")
private EntityManagerFactory emf;
#Produces
#RequestScoped
public EntityManager createEntityManager() {
return emf.createEntityManager();
}
public void disposeEntityManager(#Disposes em) {
if (em.isOpen()) {
em.close();
}
}
Note the usage of #PersistenceUnitName("myPU"), the deltaspike annotation that will handle the instanciation of the EMF.
If you have multiple persistence units, as it is often the case in the real world, you can set them apart with qualifiers. To declare a qualifier, declare an #interface with the following annotations :
#Target({ FIELD, METHOD, PARAMETER, TYPE })
#Retention(RUNTIME)
#Documented
#Qualifier
public #interface MyQualifier {
}
Then, add this qualifier to all #Produces, #Disposes and #Inject, to allow CDI to decide which persistence unit / entity manager you are willing to use :
public class MyEntityManagerProducer {
#Inject
#PersistenceUnitName("myPU")
private EntityManagerFactory emf;
#Produces
#MyQualifier
#RequestScoped
public EntityManager createEntityManager() {
return emf.createEntityManager();
}
public void disposeEntityManager(#Disposes #MyQualifier em) {
if (em.isOpen()) {
em.close();
}
}
and in your controller :
#Named
#RequestScoped
public class CustomerJpaController implements Serializable {
#Inject
#MyQualifier
private EntityManager em;
}
All this requires CDI. Configuring CDI is way beyond a short answer to your question. I use OpenWebBeans in all my projects. Weld is also very popular.
This is my understanding of things (it might not be 100% correct but it will give you a general idea) :
Where in your bean is your Service instantiated ? Nowhere. In other words customerJpa is null.
Starting a connection to a db weights a lot on resources. So instead of you instantiating different services by yourself and opening-closing connections, the container has a pool of services and give the free ones to whoever needs it (in your case your bean needs one). How do you ask the container to give you a service :
Annotate #EJB above your service:
#EJB
private CustomerJpaController customerJpa;
and I think you are missing #Stateless as well
#Stateless
public class CustomerJpaController...
It's advised to switch to #Named and #RequestScoped (the other package) instead of #ManagedBean. Then you can use #Inject to inject your service instead of #EJB.here you can read further on the subject.

getselectednode from another bean (primefaces)

I have a ManagedBean of a treeNode and other managed bean where i would like to get the selectedNode and from getType i would like to execute some code but the problem i can't get the selectedNode cause every time i get this:
java.lang.NullPointerException: javax.faces.FacesException: #{dimMan.makeDim()}: java.lang.NullPointerException
and this is my two Managed bean:
#ManagedBean
#ViewScoped
public class TreeBean implements Serializable {
private static final long serialVersionUID = 2417620239014385855L;
private TreeNode root;
private TreeNode selectedNode;
.....
and the other one where i would like to make a test of the type of selected node:
#ManagedBean(name = "dimMan")
#SessionScoped
public class DimenssionManaged {
#EJB
DimensionDaoRemote dimService;
#Inject
TreeBean treeSelected;
String select;
public TreeBean getTreeSelected() {
return treeSelected;
}
public void setTreeSelected(TreeBean treeSelected) {
this.treeSelected = treeSelected;
}
public void makeDim(){
System.out.println("adding dimen");
fkey=tTable.getSelectedFk();
dimUpdate.setFk_dimension(fkey);
dimUpdate.setType_dimension(selectedType);
select=treeSelected.getSelectedNode().getParent().getType();
System.out.println(select);
if (select=="cube"){
CubeBase cub=cubManged.getCubUpdate();
dimUpdate.setCube(cub);
dimService.creat_dimension(dimUpdate);
}
else {
SchemaBase sh=shmanged.getSchema();
dimUpdate.setSchema(sh);
dimService.creat_dimension(dimUpdate);
}
}
i try also to use this annotation #ManagedProperty(value =***) but it didn't work to so what should i do to get the selectedNode type from in other ManagedBean ?
DimenssionManaged ManagedBean associated with other page? If so once you navigate TreeBean will loose its data since its in #ViewScoped.
Change TreeBean to #SessionScoped to retain the the data even after navigation, but clearing/refreshing the data again is a concern.

CDI been creating new record instead of updating

I have changed my JSF manged bean to a CDI named bean. However I get a strange behavior that when I update a record using JPA merge() through EJB, a new record is being created instead of updating the entity.
my previous implementation
#ManagedBean
#ViewScoped
public class bean implements serializable{
#EJB Service service;
private Entity entity;
#PostConstruct
private void init(){
int id = 1;
this.entity = (Entity) service.findEntity(Entity.class, 1);
}
//invoke after editing entity
public void update(){
service.update(entity);
}
}
#Stateless
public class Service implements Serializable{
#PersistenceContext(unitName="unitName")
private EntityManager em;
public void update(Object obj){
em.merge(obj);
}
public Object find(Class klass, object pk){
return em.find(klass, pk);
}
}
Result: entity is being updated
My new implementation
#Named
#ConversationScoped
public class bean implements Serializable{
//unchanged
}
Result: entity is not being updated, and instead a new record is being created with all fields being duplicated except the id (pk) as it is an auto generated integer, and a new id is generated for the new record; Why is this happening?
Did you really want to chane the scope of your bean to ConversationScoped. I would have thought that you would use
"javax.faces.view.ViewScoped"
[not javax.faces.bean.ViewScoped!!] and just use #Named. Changing a bean scope changed the whole semantics.

How to get managedbean property from another bean in JSF

I searched similar questions but I'm a bit confused. I have a login page, so LoginBean also which is;
#ManagedBean(name = "loginBean")
#SessionScoped
public class LoginBean implements Serializable {
private String password="";
private String image="";
#ManagedProperty(value = "#{loginBeanIdentityNr}")
private String identityNr="";
...
after success, navigates to orderlist page, so I have also OrderBean.
#ManagedBean(name = "OrderBean")
#SessionScoped
public class OrderBean {
List<Ordery> sdList;
public List<Order> getSdList() {
try {
String identityNr ="";
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
LoginBean lBean = (LoginBean) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "loginBean");
identityNr =lBean.getIdentityNr();
sdList = DatabaseUtil.getOrderByIdentityNr(identityNr);
...
}
I don't need the whole LoginBean, just ManagedProperty "loginBeanIdentityNr". But this code below doesn't work (of course);
identityNr = (String) FacesContext.getCurrentInstance()
.getApplication().getELResolver()
.getValue(elContext, null, "loginBeanIdentityNr");
this time it returns null to me.
I think if I need whole bean property, I can inject these beans, right? So, do you have any suggestions for this approach? can<f:attribute> be used?
The #ManagedProperty declares the location where JSF should set the property, not where JSF should "export" the property. You need to just inject the LoginBean as property of OrderBean.
public class OrderBean {
#ManagedProperty(value="#{loginBean}")
private LoginBean loginBean; // +setter
// ...
}
This way you can access it in the OrderBean by just
loginBean.getIdentityNr();
Alternatively, if you make your OrderBean request or view scoped, then you can also set only the identityNr property.
public class OrderBean {
#ManagedProperty(value="#{loginBean.identityNr}")
private String identityNr; // +setter
// ...
}
Unrelated to the concrete problem: initializing String properties with an empty string is a poor practice.

Resources