SEVERE:org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[entities.CustomerProfile#0]

时间:2013-12-27 04:14:02

标签: java hibernate jsf jsf-2 primefaces

很抱歉,如果我的帖子重复但我无法解决其他主题的问题,那么我创建此主题。希望有人帮助我。我正在使用Hibernate,JSF和Glassfish。

表关系 enter image description here

这是我的完整代码

customerBean(requestscoped)

public class customerBean implements Serializable {

    private CustomerProfile selectedCustomer = new CustomerProfile();
    private List<CustomerProfile> customer;

    /** Creates a new instance of customerBean */
    public customerBean() {
    }

    public List<CustomerProfile> getCustomer() {
        customerDao cust_dao = new customerDao();
        customer = cust_dao.findAll();
        return customer;
    }

    public CustomerProfile getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(CustomerProfile selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public void btnUpdate(){
        customerDao cust_create = new customerDao();
        String msg;
        if(cust_create.updateCustomer(selectedCustomer)){
            msg = "Updated Successfully!";
        }else{
            msg = "Error. Please check again!";
        }
        FacesMessage massage = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
        FacesContext.getCurrentInstance().addMessage(null, massage);
    }
}

customerDao

public class customerDao {
public List<CustomerProfile> findAll(){
    List<CustomerProfile> list_cust = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    String sql = "FROM CustomerProfile";
    try{
        session.beginTransaction();
        list_cust = session.createQuery(sql).list();
        for(CustomerProfile cp : list_cust){
            Hibernate.initialize(cp.getCustomer());
            or cp.getCustomer().getLoginName();
        }
        session.beginTransaction().commit();
    }catch(Exception e){
        e.printStackTrace();
    }
    return list_cust;
}

    public boolean updateCustomer(CustomerProfile customer){
        boolean flag;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try{
            session.beginTransaction();
            CustomerProfile cust_info = (CustomerProfile) session.load(CustomerProfile.class, customer.getProfile());
            cust_info.setFullname(customer.getFullname());
            cust_info.setEmail(customer.getEmail());
            cust_info.setPhone(customer.getPhone());
            session.merge(customer);
            session.beginTransaction().commit();
            flag = true;
        }catch(Exception e){
            flag = false;
            e.printStackTrace();
            session.beginTransaction().rollback();
        }
        return flag;
    }
}

XHTML

<p:growl id="msgs" showDetail="true" />
<h:form id="formData">
<p:dataTable id="customers" var="customer_profile" value="#{customerBean.customer}" paginator="true" paginatorPosition="bottom" rows="10"
             paginatorTemplate="{PreviousPageLink} {PageLinks} {NextPageLink} {RowsPerPageDropdown}"
             rowsPerPageTemplate="5,10,15">

    <p:column headerText="Customer ID">
        <h:outputText value="#{customer_profile.customer.customId}" />
    </p:column>

    <p:column headerText="Full Name">
        <h:outputText value="#{customer_profile.fullname}" />
    </p:column>

    <p:column headerText="Phone">
        <h:outputText value="#{customer_profile.phone}" />
    </p:column>

    <p:column headerText="Email">
        <h:outputText value="#{customer_profile.email}" />
    </p:column>

    <p:column headerText="Order">
        <h:outputText value="#{customer_profile.quantityOrder}" />
    </p:column>

    <p:column headerText="Date Created">
        <h:outputText value="#{customer_profile.dateCreated}" />
    </p:column>

    <p:column>
        <p:commandButton id="btnUpdate" oncomplete="updateDialog.show()" icon="ui-icon-search" title="Update" update=":formUpdate">
            <f:setPropertyActionListener value="#{customer_profile}" target="#{customerBean.selectedCustomer}" />
        </p:commandButton>
    </p:column>

</p:dataTable>
</h:form>

<!-- Start formUpdate -->
<h:form id="formUpdate">
<p:dialog header="Customer Details" widgetVar="updateDialog" resizable="false" id="updateDlg" showEffect="fade" hideEffect="explode">

    <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">

        <h:outputText value="Profile ID: " />
        <h:outputLabel value="#{customerBean.selectedCustomer.profile}" />
        <h:outputLabel value="#{customerBean.selectedCustomer.customer.loginName}" />

        <h:outputText value="Full Name: " />
        <h:inputText value="#{customerBean.selectedCustomer.fullname}" />

        <h:outputText value="Phone: " />
        <h:inputText value="#{customerBean.selectedCustomer.phone}" />

        <h:outputText value="Email: " />
        <h:inputText value="#{customerBean.selectedCustomer.email}" />    

        <f:facet name="footer">
            <p:separator />
            <p:commandButton id="btnOK" immediate="true" oncomplete="updateDialog.hide()" action="#{customerBean.btnUpdate}" icon="ui-icon-search" title="Save" value="Save" update=":formData, :msgs" />
            <p:commandButton id="btnCancel" oncomplete="updateDialog.hide()" icon="ui-icon-search" title="Cancel" value="Cancel" />
        </f:facet>
    </h:panelGrid>

</p:dialog>
</h:form>
<!-- End formUpdate -->

Customer.hbm.xml

<hibernate-mapping>
  <class catalog="catering" name="entities.Customer" schema="dbo" table="customer">
    <id name="customId" type="int">
      <column name="customID"/>
      <generator class="assigned"/>
    </id>
    <property name="loginName" type="string">
      <column name="loginName" not-null="true" unique="true"/>
    </property>
    <property name="password" type="string">
      <column name="password"/>
    </property>
    <set inverse="true" name="customerProfiles">
      <key>
        <column name="customID"/>
      </key>
      <one-to-many class="entities.CustomerProfile"/>
    </set>
    <set inverse="true" name="orderses">
      <key>
        <column name="customID" not-null="true"/>
      </key>
      <one-to-many class="entities.Orders"/>
    </set>
  </class>
</hibernate-mapping>

Customer.java

public class Customer  implements java.io.Serializable {
     private int customId;
     private Serializable loginName;
     private Serializable password;
     private Set customerProfiles = new HashSet(0);
     private Set orderses = new HashSet(0);

    public Customer() {
    }


    public Customer(int customId, Serializable loginName) {
        this.customId = customId;
        this.loginName = loginName;
    }
    public Customer(int customId, Serializable loginName, Serializable password, Set customerProfiles, Set orderses) {
       this.customId = customId;
       this.loginName = loginName;
       this.password = password;
       this.customerProfiles = customerProfiles;
       this.orderses = orderses;
    }

    public int getCustomId() {
        return this.customId;
    }

    public void setCustomId(int customId) {
        this.customId = customId;
    }
    public Serializable getLoginName() {
        return this.loginName;
    }

    public void setLoginName(Serializable loginName) {
        this.loginName = loginName;
    }
    public Serializable getPassword() {
        return this.password;
    }

    public void setPassword(Serializable password) {
        this.password = password;
    }
    public Set getCustomerProfiles() {
        return this.customerProfiles;
    }

    public void setCustomerProfiles(Set customerProfiles) {
        this.customerProfiles = customerProfiles;
    }
    public Set getOrderses() {
        return this.orderses;
    }

    public void setOrderses(Set orderses) {
        this.orderses = orderses;
    }
}

CustomerProfile.hbm.xml

<hibernate-mapping>
  <class catalog="catering" name="entities.CustomerProfile" schema="dbo" table="customer_profile">
    <id name="profile" type="int">
      <column name="profile"/>
      <generator class="assigned"/>
    </id>
    <many-to-one class="entities.Customer" fetch="select" name="customer">
      <column name="customID"/>
    </many-to-one>
    <property name="gender" type="java.lang.Boolean">
      <column name="gender"/>
    </property>
    <property name="fullname" type="string">
      <column name="fullname" not-null="true"/>
    </property>
    <property name="phone" type="string">
      <column length="15" name="phone" not-null="true"/>
    </property>
    <property name="email" type="string">
      <column name="email"/>
    </property>
    <property name="quantityOrder" type="java.lang.Integer">
      <column name="quantityOrder"/>
    </property>
    <property name="isVegetarian" type="java.lang.Boolean">
      <column name="isVegetarian"/>
    </property>
    <property name="dateCreated" type="date">
      <column name="dateCreated"/>
    </property>
  </class>
</hibernate-mapping>

CustomerProfile.java

public class CustomerProfile  implements java.io.Serializable {
     private int profile;
     private Customer customer;
     private Boolean gender;
     private Serializable fullname;
     private String phone;
     private Serializable email;
     private Integer quantityOrder;
     private Boolean isVegetarian;
     private Serializable dateCreated;

    public CustomerProfile() {
    }


    public CustomerProfile(int profile, Serializable fullname, String phone) {
        this.profile = profile;
        this.fullname = fullname;
        this.phone = phone;
    }
    public CustomerProfile(int profile, Customer customer, Boolean gender, Serializable fullname, String phone, Serializable email, Integer quantityOrder, Boolean isVegetarian, Serializable dateCreated) {
       this.profile = profile;
       this.customer = customer;
       this.gender = gender;
       this.fullname = fullname;
       this.phone = phone;
       this.email = email;
       this.quantityOrder = quantityOrder;
       this.isVegetarian = isVegetarian;
       this.dateCreated = dateCreated;
    }

    public int getProfile() {
        return this.profile;
    }

    public void setProfile(int profile) {
        this.profile = profile;
    }
    public Customer getCustomer() {
        return this.customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public Boolean getGender() {
        return this.gender;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }
    public Serializable getFullname() {
        return this.fullname;
    }

    public void setFullname(Serializable fullname) {
        this.fullname = fullname;
    }
    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Serializable getEmail() {
        return this.email;
    }

    public void setEmail(Serializable email) {
        this.email = email;
    }
    public Integer getQuantityOrder() {
        return this.quantityOrder;
    }

    public void setQuantityOrder(Integer quantityOrder) {
        this.quantityOrder = quantityOrder;
    }
    public Boolean getIsVegetarian() {
        return this.isVegetarian;
    }

    public void setIsVegetarian(Boolean isVegetarian) {
        this.isVegetarian = isVegetarian;
    }
    public Serializable getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Serializable dateCreated) {
        this.dateCreated = dateCreated;
    }
}

我更新时遇到此错误:

  

SEVERE:org.hibernate.ObjectNotFoundException:没有给定的行   标识符存在:[entities.CustomerProfile#0]

1 个答案:

答案 0 :(得分:0)

为什么你需要merge()?合并方法的意图是关联/持久化 在某些其他事务/会话中分离的实体。但在您的情况下,您正在获取cust_info并进行更改并尝试在同一会话中进行更新。含义cust_info尚未分离且仅处于持久状态。使用update()或saveorUpdate()方法而不是merge()。

Try this.

 public boolean updateCustomer(CustomerProfile customer){
    boolean flag;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try{
 Transaction transaction  =     session.beginTransaction();
        CustomerProfile cust_info = (CustomerProfile) session.load(CustomerProfile.class, customer.getProfile());
        cust_info.setFullname(customer.getFullname());
        cust_info.setEmail(customer.getEmail());
        cust_info.setPhone(customer.getPhone());
        session.update(customer);
        //else saveOrUpdate method
        transaction.commit();
        flag = true;
    }catch(Exception e){
        flag = false;
        e.printStackTrace();
        session.beginTransaction().rollback();
    }
    return flag;
}