我正在尝试使用JSF和JPA开发一个简单的CRUD操作。我正在尝试执行更新操作,但是在尝试更新时出现此错误:
Object: null is not a known Entity type.
java.lang.IllegalArgumentException: Object: null is not a known Entity type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:548)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:530)
at org.apache.openejb.persistence.JtaEntityManager.merge(JtaEntityManager.java:203)
at it.crud.business.BookBean.updateBook(BookBean.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:191)
at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:102)
at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:205)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186)
at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:252)
at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:212)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:265)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:260)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:89)
at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:347)
at com.sun.proxy.$Proxy95.updateBook(Unknown Source)
at it.crud.view.BookManagedBean.editBook(BookManagedBean.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at org.apache.webbeans.el22.WrappedMethodExpression.invoke(WrappedMethodExpression.java:52)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:814)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(OpenEJBSecurityListener.java:97)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
index.xhtml
<h:form id="form">
<ui:dataTable value="#{bookBean.books}" var="book"
style="margin: 2em;" rowKey="#{book.id}">
<ui:column headerText="Name">
<h:outputText value="#{book.name}" />
</ui:column>
<ui:column headerText="Year">
<h:outputText value="#{book.year}" />
</ui:column>
<ui:column headerText="Author">
<h:outputText value="#{book.author}" />
</ui:column>
<ui:column headerText="Category">
<h:outputText value="#{book.category}" />
</ui:column>
<ui:column headerText="Price">
<h:outputText value="€#{book.price}" />
</ui:column>
<ui:column style="width:100px;text-align: center">
<ui:commandButton icon="fa fa-pencil" update=":form:bookEdit"
oncomplete="PF('editDialog').show()">
<f:setPropertyActionListener value="#{book}"
target="#{bookBean.selectedBook}" />
</ui:commandButton>
<ui:commandButton action="#{bookBean.removeBook(book)}"
icon="fa fa-trash"></ui:commandButton>
</ui:column>
</ui:dataTable>
<ui:dialog header="Edit Book" widgetVar="editDialog" modal="true"
showEffect="fade" hideEffect="fade" resizable="false">
<ui:outputPanel id="bookEdit" style="text-align:center;">
<ui:panelGrid columns="2"
rendered="#{not empty bookBean.selectedBook}"
columnClasses="label,value">
<h:outputText value="Name: " />
<ui:inputText value="#{bookBean.selectedBook.name}" />
<h:outputText value="Category: " />
<ui:inputText value="#{bookBean.selectedBook.category}" />
<h:outputText value="Author: " />
<ui:inputText value="#{bookBean.selectedBook.author}" />
<h:outputText value="Price: €" />
<ui:inputText value="#{bookBean.selectedBook.price}" />
<h:outputText value="Year: " />
<ui:inputText value="#{bookBean.selectedBook.year}" />
</ui:panelGrid>
<ui:commandButton value="Update" icon="fa fa-fw fa-pencil"
actionListener="#{bookBean.editBook(selectedBook)}" />
</ui:outputPanel>
</ui:dialog>
</h:form>
实体类
@Entity
@Table(name="book")
public class Book {
private Integer id_book;
private String name;
private String author;
private String category;
private String year;
private String price;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_book")
public Integer getId_book() {
return id_book;
}
public void setId_book(Integer id_book) {
this.id_book = id_book;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Column(name="category")
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Column(name="year")
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
@Column(name="price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Bean类
@Stateless
public class BookBean implements BookLocal, BookRemote {
@PersistenceContext(unitName="CRUDPersistence")
private EntityManager entityManager;
...
public void updateBook(Book book) {
this.entityManager.merge(book);
}
...
}
ManagedBean
@ManagedBean(name="bookBean")
@RequestScoped
public class BookManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
private Book b;
private Book selectedBook;
@EJB
private BookLocal bookBusiness;
public BookManagedBean() {
b = new Book();
}
public Book getB() {
return b;
}
public void setB(Book b) {
this.b = b;
}
public Book getSelectedBook() {
return selectedBook;
}
public void setSelectedBook(Book selectedBook) {
this.selectedBook = selectedBook;
}
public String editBook(Book b) {
bookBusiness.updateBook(b);
return "index.xhtml?faces-redirect=true";
}
}
调试应用程序时,我注意到方法b
中的变量public String editBook(Book b)
为空,但是我不明白原因。问题出在哪里?谢谢!