Reputation: 1203
I am using JSF - EJB3 - Hibernate JPA2.0 in my application. In one of the screens when I try to persist a new entry, I get the following exception:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: info.novatec.timemgmt.entities.Customer
Following are chunks of my code that may be helpful,
View:
<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>
Managed bean
@ManagedBean
@SessionScoped
public class CustomerController implements Serializable {
// ...
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
// ...
}
JSFUtil
helper class:
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;
}
}
Could you please point as to where I am going wrong?
Upvotes: 0
Views: 1029
Reputation: 1108972
The problem is in your Converter
for the Customer
class (which you omitted from the question, but it's surely there in your real code). You seem to be manually constructing a new Customer()
with an ID instead of obtaining the Customer
instance from entity manager by its ID. Fix the converter accordingly.
Upvotes: 1