• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA TransactionRequiredException

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a simple JSF 2.3 application and I am trying to use JPA to persist my data to the database.

I am using TomEE 8.0.6.

I am not using Spring.  I am not using EJB.

Unfortunately, I am getting a "Transaction required" exception when I try to persist data to the database



My configuration files
/src/META-INF/persistence.xml


/WebContent/WEB-INF/resources.xml


My Java files




My JSF page
/WebContent/addStudentForm.xhtml


When I call studentController.addStudent(student) the error gets thrown on the line entityManager.persist(student);

I know the DB connection is working because the following page calls studentController.loadStudents() and displays a list of the students.

/WebContent/addStudentList.xhtml


Any ideas on how to get the transactions working?

Many thanks
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why you're using TomEE if you're not using EJB, If all you want is JPA, ordinary Tomcat can do that without the overhead. And remember, JPA is a subset of EJB3!

A note on your "action". That's not how JSF wants actions to work. An action method is supposed to be a method that takes no parameters and returns a String (which indicates which View to display next). For "click on link/button in dataTable, you should wrap your table's Model Data in a DataModel object, which will decorate it with some necessary infrastructure. Then, when your (proper!) action method is fired, it can easily determine which row was selected by using the DataModel's getCurrentRow() or getRowIndex() methods. The action method is supposed to be indicated by a reference (action="#{studentController.addStudent}") not a "call". Putting an explicit method call in an EL expression may cause it to be invoked at the wrong point in the JSF lifecycle.

OK, enough nitpicking. I've discovered that JPA can often demand a transaction for things that you wouldn't consider transactional (like SELECTS). I've never found out why, just take it as a given.

Since you're not using an implicit transaction management framework such as Spring Data, that means that you're going to have to wrap your database operations in explicit getTransaction/begin/commit/rollback calls.

Finally, I've seen indications that when you create a backing bean using NetBeans, the name it gives ends with "Controller". This is a lie. Backing beans are Models. Controller logic in JSF resides in the FacesServlet and in the tag library implementations. Unless you're creating a custom tag using low-level logic in JSF, you never write Controller logic in JSF. You create Models and View Templates only. I think some of the confusion may be because backing beans can contain Action Methods, but Action methods are not Controller code. Controllers move data values between Model and View. Action methods see data already updated in the Model. Action methods are more properly classified as "business logic" and are invoked after all Controllers update the bean and before Controllers post any action updates to bean properties back to the View. So a Backing Bean is an impure Model (with business logic), but never a Controller.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic