Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Sunday, November 20, 2011

Testing LAZY mechanism in EJB 3


Understanding the difference between lazy and eager loading it is an important asspect for optimal querying database tables involved in relatioships of type one-to-one, one-to-many and many-to-many. 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 table 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 LAZY mechanism ! Hope you like it.

Tuesday, November 15, 2011

Two Tips About CDI Injection


CDI injection is a well-know and cover subject. Injecting beans through CDI is a common approach and everything works just fine. Anyway, two small tips can be useful sometimes:

1.Injecting instance of a bean can be sometimes tricky if you keep in mind that CDI injects contextual beans, therefore possible shared beans. You can fix this by using the @javax.enterprise.inject.New annotation that indicates that a new instance should be injected.

public class SomethingNew {
@Produces public SomethingYap getYap(@New Yap yap)
return new SomethingYap(yap);
}

2.Well, the second tip is related of fact that injection occurs when a component is created and initialized by CDI. When this behavior is not ok with you, then programmatic lookup is available by injecting the corresponding javax.enterprise.inject.Instance for a bean and using the get() method that do the lookup defined by the injection point:

public class SomethingNew {
@Inject Instance yap;
public Yap getYap() {
return yap.get();
}
}

Monday, November 14, 2011

Reducing [a,b] interval to [c,d] interval




package interval;

public class Interval {

public static float interval(float a, float b, float c, float d, float x) {

float deltaab, deltadc, r = 0;
deltaab = b - a;
deltadc = d - c;

r = c + ((x - a) / deltaab * deltadc);

return r;
}

public static void main(String[] args) {

float result = interval(1, 10, 1, 20, 10);
System.out.println("Result: " + result);
}
}