我在我的应用程序中使用JSF - EJB3 - Hibernate JPA2.0。在我尝试保留新条目的其中一个屏幕中,我得到以下异常:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: info.novatec.timemgmt.entities.Customer
以下是我的代码块,可能会有所帮助,
查看:
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Customer:" for="customer" />
<h:selectOneMenu id="customer" value="#{projectController.selected.customer}" title="Customer" >
<f:selectItems value="#{customerController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Name:" for="name" />
<h:inputText id="name" value="#{projectController.selected.name}" title="Name" />
<p:calendar id="endDate" value="#{projectController.selected.endDate}" showOn="button" pattern="MM/dd/yyyy" size="10"/>
</h:panelGrid>
托管bean
@ManagedBean
@SessionScoped
public class CustomerController implements Serializable {
// ...
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
// ...
}
JSFUtil
助手类:
public class JsfUtil{
public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
int size = selectOne ? entities.size() + 1 : entities.size();
SelectItem[] items = new SelectItem[size];
int i = 0;
if (selectOne) {
items[0] = new SelectItem("", "---");
i++;
}
for (Object x : entities) {
items[i++] = new SelectItem(x, x.toString());
}
return items;
}
}
你能否指出我哪里出错了?
答案 0 :(得分:1)
问题出现在Converter
类的Customer
中(您从问题中省略了,但它确实存在于您的真实代码中)。您似乎手动构建带有ID的new Customer()
,而不是通过其ID从实体管理器获取Customer
实例。相应地修复转换器。