抱歉,如果我的帖子是重复的,但我不能从另一个主题解决我的问题,所以我创建了这个主题。希望有人帮助我。我用的是Hibernate,JSF和Glassfish。
这是我的完整代码
CusterBean(请求范围)
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);
}
}
顾客道
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 -->
我得到这个错误:
SEVERE:javax.el.ELException: /admin/customer_list.xhtml@68,106 value="#{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;
}
}
客户资料. 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;
}
}
这可能不相关,但我发现这种模式有问题。
try{
session.beginTransaction();
// do something
session.beginTransaction().commit();
}catch(Exception e){
session.beginTransaction().rollback();
}
你不应该在交易开始时打3次电话。< br >我的意思是,我怀疑您的代码是否达到了您的预期目的。< br >
查看此页面顶部,了解此模式的外观。
http://docs.jboss.org/hibernate/annotations/3.5/api/org/hibernate/Session.html
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;
}
问题内容: 我有2台物理服务器,我的Web应用程序命中该服务器由负载均衡器管理。我总是得到- org.hibernate.LazyInitializationException:无法初始化代理-没有会话 当其中一台服务器受到攻击而另一台服务器运行平稳而没有任何问题时。我有一个由应用程序启用和管理的本地托管缓存存储。仅当尝试从一个表访问一个特定的列时,才会发生此异常。不管选择哪个服务器,其余的操作都
问题内容: 我试图从数据库中的对象进行简单加载,但出现错误“无法初始化代理-没有会话”,知道吗?谢谢 问题答案: 尝试添加到validate方法: 发生的事情是,因为没有注释,所以没有与该方法关联的会话,并且每个查询都将在其自己的会话中运行,该会话随后将立即关闭。 该方法始终返回一个代理,与之不同(请参阅此处,了解load与get之间的差异)。 因此,返回了代理,但是由于缺少代理,因此立即关闭了创
我试图从DB中的一个对象做一个简单的加载,但是我得到的错误是“Could not initialize proxy-no session”,有什么想法吗?谢谢
我正在尝试使用Primeface JSF Spring Boot做一个示例User CRUD页面。在页面上,我有一个启用LazyLoding的表。我的User对象没有1对N或N对1字段,都是原始字段,在到达时不需要数据库访问或初始化。(所以FetchType.EAGER不会有帮助) 当试图在弹出窗口上显示用户列表中的用户时,出现以下异常: 为什么 JSF 组件尝试从数据库中更新对象以获取 1 比
问题内容: 对数据库有以下查询: 在获取employee.address.street,employee.address.houseNumber或employee.address.city时,它将失败,但以下情况除外: 员工映射: 地址映射: 对于其他类(办公室,公司等),这是绝对正常的。如果注释,则在jsp应用程序中加载地址字段的行将正常工作。怎么了?顺便说一下,尽管有异常,它仍显示有关js
问题内容: 我的代码检索与用户有关的所有信息: 在简单地返回一组用户的ES。 我的问题是:即使会话已经关闭,为什么对象仍然具有其值(例如名称)?是该类的实例变量。但是为什么我不能检索其值却可以检索该类的常规实例变量? 是一个。 问题答案: 有关使用惰性关联的hibernate文档清楚地将这种访问称为错误。只有在会话仍处于打开状态时,才能与延迟关联的对象进行交互。文档的该部分还提供了访问对象的延迟关