Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing the lazy mechanism in EJB 3

DZone's Guide to

Testing the lazy mechanism in EJB 3

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Understanding the difference between lazy and eager loading it is an important aspect for optimal querying of database tables involved in one-to-one, one-to-many or many-to-many relationships. In this post, I will use EJB 3 to query data from such a database using the lazy loading mechanism. In our database we have two tables, teams which stores some football teams, and  players which contains the players of each team. As a relationship, we will use the one-to-many bidirectional relationship, this means that one team from teams table has one or more players from players table and each player belongs to a single team.

Below is the code of our three entites involved in the one-to-many bidirectional relationship. First, Teams.java:

. . .
@Entity
@Table(name = "teams")
@XmlRootElement
@NamedQueries({@NamedQuery(name = "Teams.findAll", query = "SELECT t FROM Teams t")})
public class Teams implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Size(max = 45)
    @Column(name = "team")
    private String team;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "teams", 
fetch = FetchType.LAZY
)
    private List playerslist;
    @XmlTransient
    public List getPlayerslist() {
        return playerslist;
    }
    public void setPlayerslist(List playerslist) {
        this.playerslist = playerslist;
    }
. . .
// getters and setters
. . .


Second, Players.java:
. . .
@Entity
@Table(name = "players")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Players.findAll", query = "SELECT p FROM Players p")})
public class Players implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected PlayersPK playersPK;
    @Size(max = 45)
    @Column(name = "name")
    private String name;
    @Size(max = 45)
    @Column(name = "surname")
    private String surname;
    @Column(name = "age")
    private Integer age;
    @JoinColumn(name = "Teams_id", referencedColumnName = "id", insertable = false,
                       updatable = false)
    @ManyToOne(optional = false, 
fetch = FetchType.LAZY
)
    private Teams teams;
. . .
// getters and setters
. . .

And the third one, PlayersPK.java:
. . .
@Embeddable
public class PlayersPK implements Serializable {

    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private int id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Teams_id")
    private int teamsid;
. . .
// getters and setters
. . .

The type of loading is marked using the fetch element as you see in bold above. In this case, lazy loading will retrieve related entites only as needed, while eager loading attemps to retrieve all related entites as once. But, is this really works?

In our stateless bean we will define a method which will get us a list of Teams records:
. . .
@Stateless
@LocalBean
public class Calls {

    @PersistenceContext(unitName = "Ucl-one-to-many-ejbPU")
    private EntityManager manager;

    public List teamsFindAll() {
        Query query = manager.createNamedQuery("Teams.findAll", Teams.class);
        if (query.getResultList().isEmpty() == false) {
            return query.getResultList();
        }
        return null;
    }
} 
. . .

Now, we use JSF for render the extracted tables. Notice that we render only the teams, not the players, which means that the LAZY mechanism should do its work and not extract the players from the database! The JSF managed bean is quite simple:
.. .
@ManagedBean
@RequestScoped
public class UclTeams {
    @EJB
    private Calls records;
    private List teams = new ArrayList();

    public List getTeams() {
        this.teams = records.teamsFindAll();
        return teams;
    }

    public void setTeams(List teams) {
        this.teams = teams;
    }
. . .

To demonstrate the lazy mechanism in our index.xhtml page we will first have:
. . .
            <h1>Testing LAZY mechanism in EJB 3</h1>
            <h:form>
                <h:dataTable value="#{uclTeams.teams}" var="a"
                             styleClass="table"
                             headerClass="header"
                             rowClasses="row-one,row-two">
                    <h:column>
                        <f:facet name="header">id</f:facet>
                        #{a.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">team</f:facet>
                        #{a.team}
                    </h:column>
                    <h:column>
                        <f:facet name="header">players</f:facet>
                        #{a.playerslist}
                    </h:column> 
                </h:dataTable>
            </h:form>
. . .

When running, into the players column we will get the message „IndirectList: not instantiated”. This is because lazy loading would ensure that the related records were not retrived until they are requested – therefore LAZY is working.


Adding code for displaying players information will cause LAZY to load the players of the requested teams also:
. . .
                    <h:column>
                        <f:facet name="header">team</f:facet>
                        #{a.team}
                    </h:column>
                    <h:column>
                        <f:facet name="header">players</f:facet>
                        <h:dataTable value="#{a.playerslist}" var="b"
                                     styleClass="table"
                                     headerClass="header-two"
                                     rowClasses="row-one,row-two">
                            <h:column>
                                <f:facet name="header">name</f:facet>
                                #{b.name}
                            </h:column>
                            <h:column>
                                <f:facet name="header">surname</f:facet>
                                #{b.surname}
                            </h:column>
                            <h:column>
                                <f:facet name="header">age</f:facet>
                                #{b.age}
                            </h:column>
                        </h:dataTable>
                    </h:column>
. . .

And the result will be:

That’s was a simple test for the lazy mechanism ! Hope you like it.

 

From http://e-blog-java.blogspot.com/2011/11/testing-lazy-mechanism-in-ejb-3.html

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}