Koray Tugay
Koray Tugay

Reputation: 23800

Can I inject a POJO to a BackingBean ? And a @Entity to a POJO? How does it work?

So this is my code:

<h:form>
    <p:dataTable var="customer" value="#{customersTableBackingBean.allCustomers}">
        <p:column headerText="First Name">
            <h:outputText value="#{customer.contactFirstName}" />
        </p:column>

        <p:column headerText="City">
            <h:outputText value="#{customer.city}" />
        </p:column>
    </p:dataTable>
</h:form>

BackingBean:

package com.tugay.maythirty.model;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;

@Named
public class CustomersTableBackingBean implements Serializable {

    @Inject
    CustomersService customersService;

    public List<Customers> getAllCustomers(){
        return customersService.getAllCustomers();
    }

    public String sayHello(){
        return "Hello from a managed bean!";
    }
}

Service:

import javax.inject.Inject;
import java.io.Serializable;
import java.util.List;

public class CustomersService implements Serializable {

    @Inject
    private CustomersDao customersDao;

    public List<Customers> getAllCustomers(){
        boolean userLoggedIn = true;
        if(userLoggedIn){
            return customersDao.getAllCustomers();
        }
        else{
            return null;
        }
    }
}

Dao:

import javax.persistence.*;
import java.util.List;

public class CustomersDao {

    @PersistenceContext(unitName = "Persistence")
    private EntityManager em;

    public List<Customers> getAllCustomers() {
        TypedQuery<Customers> query = em.createQuery("SELECT c FROM Customers c", Customers.class);
        List<Customers> allCustomers = query.getResultList();
        return allCustomers;
    }

    public void addCustomer(String customerName, String city){
        Customers customer = new Customers();
        customer.setCustomerName(customerName);
        customer.setCity(city);
        em.persist(customer);
    }
}

Entity:

@Entity
public class Customers {
    //relevant code here...

So this runs on Glassfish server, and runs just fine.. I can fetch the data from the db and see it on index.xhtml..

My backing mean is a @Named bean. So @Named makes this be accessible from the EL.

However, it does not have any other annotations, such as Statless or Stateful.. So what is the "state" of this backing bean? Is the lifetime of this bean not managed by the container?

Also, as you can see I am injecting a Service to my BackingBean with @Inject,and a Dao to my Service, and an Entity to my Dao... Are these EJB 's? Why would I want to make these @Stateless or @Stateful if they work just fine like this?

Why would/should I make any of these beans @Stateful or @Statless?

Upvotes: 1

Views: 553

Answers (2)

LightGuard
LightGuard

Reputation: 5378

Per the CDI spec (sorry on my phone, can't look up the section easily) without any context / scope annotations the bean is DependantScoped, which means the container creates a new instance every time it's accessed (or created and set on the containing bean, similar to doing new yourself).

The other question about Entities, you should create a producer for each entity you want to directly access on the front end. If you don't, it may not save correctly on persist.

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

CustomersService is just a POJO. If you annotate it with @Stateful then it will be a statefull EJB (with @Statless - a stateless EJB). Unlike a POJO (which is not an EJB), an EJB is managed by the EJB container, and can benefit with the facilities (e.g. container managed transactions, security, ...) that the EJB container has to offer.

But if you don't require the services provided by EJB container then I think there is no point in making them EJBs.

So what is the "state" of this backing bean?

IMO, that doesn't quite make sense. I think you mean the scope (e.g. request, session, application, ...) of the bean. If so, since you haven't specified any scope for it, I think it won't be put in any of the scopes. It will be instantiated whenever needed (e.g. in this case it should be instantiated every time the page is requested). But if it was scoped then an instance of the bean would live as long as the scope itself would live. e.g. a request scoped bean is instantiated for each new request.

Here is the tutorial for EJB.

Above tutorial link is for Java EE 6. Thanks to @Luiggi for the Java EE 7 tutorial link.

Upvotes: 4

Related Questions