当我试图添加评论时,我收到此错误,告诉我应该在事务处于活动状态时执行此操作,尽管我在事务中执行持久性。
奇怪的是,我以与我在项目中拥有的所有其他实体相同的方式进行此操作,除此之外,它们的工作正常。
javax.el.ELException: <openjpa-2.3.0-nonfinal-1540826-r422266:1542644 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException: Can only perform operation while a transaction is active.
viewId=/book_info.xhtml
location=C:\Users\Admin\Documents\NetBeansProjects\Ebook_store\target\Ebook_store-1.0-SNAPSHOT\book_info.xhtml
phaseId=INVOKE_APPLICATION(5)
Caused by:
org.apache.openjpa.persistence.InvalidStateException - Can only perform operation while a transaction is active.
at org.apache.openjpa.kernel.BrokerImpl.assertTransactionOperation(BrokerImpl.java:4735)
/sections/container_bookinfo.xhtml at line 52 and column 98 action="#{reviewActionBean.addReview()}"
<HtmlCommandButton action="#{reviewActionBean.addReview()}" actionExpression="#{reviewActionBean.addReview()}" class="class javax.faces.component.html.HtmlCommandButton" clientId="j_id_p:j_id_11" disabled="false" id="j_id_11" immediate="false" inView="true" readonly="false" rendered="true" transient="false" type="submit" value="Submit review" location="/sections/container_bookinfo.xhtml at line 52 and column 98"/> - State size:0 bytes
审核bean是一个标准的Netbeans生成的数据库实体,我试图坚持下去。
动作bean:
@ManagedBean
@SessionScoped
public class ReviewActionBean implements Serializable{
private static final long serialVersionUID = 7826937573658271958L;
private static Logger logger = Logger.getLogger(ReviewActionBean.class.getName());
private EntityManagerFactory factory = Persistence.createEntityManagerFactory("com.ebook_Ebook_store");
private EntityManager entityManager = factory.createEntityManager();
private List<Review> reviewList;
private int bookId;
private Review review;
public List<Review> getReviews() {
bookId = ((Book)getFromSessionScope("book")).getBookId();
return entityManager.createQuery("SELECT r FROM Review r WHERE r.bookId = :bookId", Review.class)
.setParameter("bookId", bookId).getResultList();
}
public boolean addReview() {
review = (Review)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("review");
review.setBookId((Book)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("review"));
review.setApprovalStatus(true);
review.setDate(new Date());
try{
entityManager.getTransaction().begin();
entityManager.persist(review);
entityManager.getTransaction().commit();
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
entityManager.getTransaction().rollback();
return false;
}
return true;
}
}
JSF视图:
<ui:composition>
<div id="bookinfo" style="margin-left: 450px;">
<div id="bookimage" style="float: left; ">
<!--<h:outputText>Book id: #{book.bookId}</h:outputText>-->
<h:graphicImage library="images" name="#{book.image}"/>
</div>
<div id="bookdiscription" style="font-family: Verdana; margin-left: 250px;">
<h:panelGrid >
<div style="margin-bottom: 10px">Book title: #{book.booktitle}</div>
<div style="margin-bottom: 10px">Author: #{book.author} </div>
<div style="margin-bottom: 10px">ISBN-number: #{book.isbn} </div>
<div style="margin-bottom: 10px">Publisher: #{book.publisher} </div>
<div style="margin-bottom: 10px">Number of pages: #{book.numberOfPages} </div>
<div style="margin-bottom: 10px">Genre: #{book.genre} </div>
<div style="margin-bottom: 10px">Wholesale price: #{book.wholesalePrice} </div>
<div style="margin-bottom: 10px">List price: #{book.listPrice} </div>
<div style="margin-bottom: 10px">Date added: #{book.dateAdded} </div>
</h:panelGrid>
</div>
</div>
<div id="review" style="margin-top: 50px; margin-left: 430px">
<h:outputLabel style="color: black; font-family: Verdana; font-size: 18px; font-weight: bold;">Add your review:</h:outputLabel>
<h:form>
<div style="margin-top: 20px;">
<h:outputLabel value="Name:"/>
<h:inputText value="#{review.name}"/>
<h:selectOneMenu value="#{review.rating}" style="margin-left: 10px">
<f:selectItem itemValue="1" itemLabel="1 star" />
<f:selectItem itemValue="2" itemLabel="2 star" />
<f:selectItem itemValue="3" itemLabel="3 star" />
<f:selectItem itemValue="4" itemLabel="4 star" />
<f:selectItem itemValue="5" itemLabel="5 star" />
</h:selectOneMenu>
</div>
<h:inputTextarea style="width: 500px; height: 100px" value="#{review.text}"/>
<h:commandButton value="Submit review" action="#{reviewActionBean.addReview()}"/>
</h:form>
</div>
</ui:composition>
</h:body>