Java Reference
In-Depth Information
Add the new
Section
instance to the
ArrayList
and flush the changes to
the database:
sectionList.add(section);
em.flush();
Similarly, in the
createArticle
method create an
Article
entity object and
persist and flush the entity instance to the database. As the
Article
entity has
many-to-one relationships with the
Edition
entity and the
Section
entity, we
also need to add the
Article
entity instance to the
Edition
and
Section
entities.
The
createArticle
method has arguments for the edition ID and the section
ID. Retrieve the
Edition
and
Section
entity objects using
getEditionById
and
getSectionById
methods and add the
Article
entity to the
Edition
and
Section
entities as explained for adding a
Section
entity instance to an
Edition
entity. The
session bean class
EJB3SessionEJBBean
is listed as follows:
package model;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless(name = "EJB3SessionEJB",
mappedName = "EJB3RelationshipsJSF-Model-EJB3SessionEJB")
@Remote
public class EJB3SessionEJBBean implements EJB3SessionEJB {
@PersistenceContext(unitName = "em")
private EntityManager em;
public EJB3SessionEJBBean() {
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Edition> getAllEditions() {
ArrayList<Edition> editions = new ArrayList<Edition>();
Query q = em.createNamedQuery("findEditionsAll");
for (Object edition : q.getResultList()) {
editions.add((Edition) edition);
}
return editions;
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
Search WWH ::
Custom Search