hibernate - “org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话”

时间:2013-12-26 17:46:40

标签: 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();
            session.beginTransaction().commit();
        }catch(Exception e){
            session.beginTransaction().rollback();
        }
        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 -->

我收到了这个错误:

  

严重:javax.el.E​​LException:/admin/customer_list.xhtml @ 68,106   值= “#{customerBean.selectedCustomer.customer.loginName}”:   org.hibernate.LazyInitializationException:无法初始化代理    - 没有会话

更新

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;
    }
}

2 个答案:

答案 0 :(得分:2)

当你与其他实体有关系时,Hibernate使用代理对象,这个代理只在需要时才有助于从DataBase获取信息,因此它的机制称为延迟初始化,并获取它需要会话对象的信息,列表中的对象客户未初始化,因此您需要这样做,明确如此:

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){
            session.beginTransaction().rollback();
        }
        return list_cust;
    }

答案 1 :(得分:0)

这可能是无关的,但我发现这种模式存在问题。

    try{
        session.beginTransaction();
        // do something
        session.beginTransaction().commit();
    }catch(Exception e){
        session.beginTransaction().rollback();
    }

你不应该打电话3次beginTransaction 我的意思是,我怀疑你的代码是否符合你的要求。

检查此页面顶部以查看此模式的外观。

http://docs.jboss.org/hibernate/annotations/3.5/api/org/hibernate/Session.html