Java 冬眠——”;org.hibernate.LazyInitializationException:无法初始化代理-无会话“;
抱歉,如果我的帖子重复,但我无法从其他主题解决我的问题,所以我创建了此主题。希望有人能帮助我。我正在使用Hibernate、JSF和Glassfish 表关系 这是我的全部代码 customerBean(请求范围) CustomerProfile.hbm.xmlJava 冬眠——”;org.hibernate.LazyInitializationException:无法初始化代理-无会话“;,java,hibernate,jsf,jsf-2,primefaces,Java,Hibernate,Jsf,Jsf 2,Primefaces,抱歉,如果我的帖子重复,但我无法从其他主题解决我的问题,所以我创建了此主题。希望有人能帮助我。我正在使用Hibernate、JSF和Glassfish 表关系 这是我的全部代码 customerBean(请求范围) CustomerProfile.hbm.xml <p:growl id="msgs" showDetail="true" /> <h:form id="formData"> <p:dataTable id="customers" var="custom
<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 -->
<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>
<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>
Hibernate在您与其他实体有关系时使用代理对象,此代理仅在需要时帮助从数据库获取信息,因此它的机制称为延迟初始化,并且要获取会话对象所需的信息,列表中的对象客户未初始化,因此您需要执行此操作,因此:
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;
}
公共列表findAll(){
List List_cust=null;
会话会话=HibernateUtil.getSessionFactory().getCurrentSession();
字符串sql=“来自CustomerProfile”;
试一试{
session.beginTransaction();
list_cust=session.createQuery(sql.list();
对于(客户档案cp:list_cust){
初始化(cp.getCustomer());
//或cp.getCustomer().getLoginName();
}
session.beginTransaction().commit();
}捕获(例外e){
session.beginTransaction().rollback();
}
退货清单;
}
这可能不相关,但我发现这种模式有问题
try{
session.beginTransaction();
// do something
session.beginTransaction().commit();
}catch(Exception e){
session.beginTransaction().rollback();
}
您在开始通话时不应打3次电话。我的意思是,我怀疑你的代码是否达到了你的目的。
查看此页面顶部以查看此模式的外观
上面说“客户在entities.CustomerProfile中拥有私人访问权限”,您可以尝试使用或cp.getCustomer().getLoginName()代替Hibernate.initializeThank现在我可以从customer表中获取信息了。但我还有两个问题。1) 为什么只需要cp.getCustomer().getLoginName()。2) 当我更新客户信息时,在调用代理对象(客户)的getter时,会出现以下错误:“严重:org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[entities.CustomerProfile#0]”第一个问题(我更新答案以使用hibernate.initialize)只有在那一刻,hibernate才会在数据库中进行选择,以获取该字段的信息。因此,这可以回答第二个问题客户id字段从未初始化我不确定代理对象在hibernate中如何工作,但在其他ORM中,如eclipse link或openjpa,当您访问代理的字段时,所有对象都已填充,也许这不是hibernate的情况,所以试着用hibernate.initialize来更新我的答案。谢谢迭代部分@peter petrov当我更新客户信息时,出现以下错误“严重:org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[entities.CustomerProfile#0]”
<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>
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;
}
}
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;
}
try{
session.beginTransaction();
// do something
session.beginTransaction().commit();
}catch(Exception e){
session.beginTransaction().rollback();
}