javax.persistence.TransactionRequiredException in small facelet application

You’re abusing a CDI managed bean as a business service. It has no clues of transaction management. You’d need to manually manage transactions. As this is usually a pain and you’re apparently using Glassfish, which is a fullworthy Java EE container supporting EJBs, you’d rather like to use a fullworthy EJB for this. When using EntityManager inside an EJB, the container will fully transparently manage DB transactions. One EJB method call counts as a single transaction (i.e. when you fire multiple DB queries and one of them fails, then everything will be automatically rolled back).

Overall, you seem to be mixing the responsibilities of the model, controller and service. Do not make your entity a managed bean as well. You should further absolutely also not perform business logic in a Javabean getter method (e.g. getBooks()). When referenced in an iterating component, it’s invoked during every iteration round. So imagine that you’ve 100 records, then the DB will be hit 100 times. This is plain inefficient.

Here’s how it should all look like:

Model (the entity):

@Entity
@Table(name = "BOOKS")
public class Book implements Serializable {
    // ...
}

Controller (the backing bean):

@Named
@RequestScoped
public class BookController {

    private Book book;
    private List<Book> books;

    @EJB
    private BookService service;

    @PostConstruct
    public void init() {
        book = new Book();
        books = service.list();
    }

    public void add() {
        service.save(book);
        init();
    }

    public Book getBook() { 
        return book;
    }

    public List<Book> getBooks() {
        return books;
    }

}

Service (the EJB):

@Stateless
public class BookService {

    @PersistenceContext
    private EntityManager em;

    public List<Book> list() {
        return em.createQuery("FROM Book", Book.class).getResultList();
    }

    public Book find(Integer id) {
        return em.find(Book.class, id);
    }

    public Integer save(Book book) {
        em.persist(book);
        return book.getId();
    }

    public void update(Book book) {
        em.merge(book);
    }

    public void delete(Book book) {
        em.remove(em.contains(book) ? book : em.merge(book));
    }

}

View (the Facelet; simplified):

<h:inputText id="title" value="#{bookController.book.title}"/>
<h:inputText id="author" value="#{bookController.book.author}"/>
<h:inputText id="price" value="#{bookController.book.price}"/>
<h:commandButton value="Add" action="#{bookController.add}" />
...
<h:dataTable value="#{bookController.books}" var="book">
    <h:column><f:facet name="header">ID</f:facet>#{book.id}</h:column>
    <h:column><f:facet name="header">Title</f:facet>#{book.title}</h:column>
    <h:column><f:facet name="header">Author</f:facet>#{book.author}</h:column>
    <h:column><f:facet name="header">Price</f:facet>#{book.price}</h:column>
</h:dataTable>

(your edit and delete buttons didn’t make any sense, so I removed them, you might want to put them inside the data table)

See also:

Leave a Comment