DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Testing the lazy mechanism in EJB 3

Testing the lazy mechanism in EJB 3

A. Programmer user avatar by
A. Programmer
·
Nov. 24, 11 · Interview
Like (0)
Save
Tweet
Share
7.52K Views

Join the DZone community and get the full member experience.

Join For Free

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

Relational database Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Metrics Part 2: The DORA Keys
  • Mission-Critical Cloud Modernization: Managing Coexistence With One-Way Data Sync
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Collaborative Development of New Features With Microservices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: