Java Reference
In-Depth Information
this.id = id;
this.taskText = taskText;
this.timeCreated = timeCreated;
}
...
The remainder of the ToDo class contains the usual getters, setters, and other entity meth-
ods.
The RequestBean class injects the EntityManager generated by the producer meth-
od, annotated with the @UserDatabase qualifier:
@ConversationScoped
@Stateful
public class RequestBean {
@Inject
@UserDatabase
EntityManager em;
It then defines two methods, one that creates and persists a single ToDo list item, and an-
other that retrieves all the ToDo items created so far by creating a query:
Click here to view code image
public ToDo createToDo(String inputString) {
ToDo toDo;
Date currentTime = Calendar.getInstance().getTime();
try {
toDo = new ToDo();
toDo.setTaskText(inputString);
toDo.setTimeCreated(currentTime);
em.persist(toDo);
return toDo;
} catch (Exception e) {
throw new EJBException(e.getMessage());
}
}
public List<ToDo> getToDos() {
try {
List<ToDo> toDos =
(List<ToDo>) em.createQuery(
"SELECT t FROM ToDo t ORDER BY t.timeCreated")
.getResultList();
return toDos;
Search WWH ::




Custom Search