在学习Hibernate过程中遇到的问题! - the owning Session was closed

本文介绍了解决Hibernate中LazyInitializationException异常的方法。通过在DAO层设置临时变量或替换load方法为get方法来避免session关闭后代理对象无法获取值的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

在写业务类代码的时候老是出现这个问题,从网上查了相关的资料解决了这个问题

这是业务类的代码

public Customers getCustomers() throws Exception
   {
     Session session = HibernateSessionFactory.currentSession();
     Transaction tx = null;
     try {
       tx = session.beginTransaction();
       Customers customer=(Customers)session.load(Customers.class,new Long(1));
      // if(!Hibernate.isInitialized(customer))
       // Hibernate.initialize(customer);

       tx.commit();
       return customer;
     }catch (Exception e) {
       if (tx != null) {
         tx.rollback();
       }
       throw e;
     } finally
     {
      session.close();
     }
   }

现把解决方案写下来:

  1。因为返回的是代理对象,在查询中使用了load方法,但是这个方法在session关闭以后代理对象也就取不到值了,所以在我的dao层的时候,代理对象还没有关闭,在这个层设置一个临时变量,把返回的结果赋给临时变量就可以了。

  2。另一种方法就是不用load方法,而改用get方法,就不会出现这种问题了。

<ScapulothoracicJoint name="default"> <!--List of components that this component owns and serializes.--> <components /> <!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The parent frame for the joint.).--> <socket_parent_frame></socket_parent_frame> <!--Path to a Component that satisfies the Socket 'child_frame' of type PhysicalFrame (description: The child frame for the joint.).--> <socket_child_frame></socket_child_frame> <!--List containing the generalized coordinates (q's) that parameterize this joint.--> <coordinates> <Coordinate name="_coord_0"> <!--List of components that this component owns and serializes.--> <components /> <!--The value of this coordinate before any value has been set. Rotational coordinate value is in radians and Translational in meters.--> <default_value>0</default_value> <!--The speed value of this coordinate before any value has been set. Rotational coordinate value is in rad/s and Translational in m/s.--> <default_speed_value>0</default_speed_value> <!--The minimum and maximum values that the coordinate can range between. Rotational coordinate range in radians and Translational in meters.--> <range>-10 10</range> <!--Flag indicating whether or not the values of the coordinates should be limited to the range, above.--> <clamped>false</clamped> <!--Flag indicating whether or not the values of the coordinates should be constrained to the current (e.g. default) value, above.--> <locked>false</locked> <!--If specified, the coordinate can be prescribed by a function of time. It can be any OpenSim Function with valid second order derivatives.--> <prescribed_function /> <!--Flag indicating whether or not the values of the coordinates should be prescribed according to the function above. It is ignored if the no prescribed function is specified.--> <prescribed>false</prescribed> <!--Flag identifies whether or not this coordinate can change freely when posing the model to satisfy kinematic constraints. When true, the coordinate's initial or specified value is ignored when considering constraints. This allows values for important coordinates, which have this flag set to false, to dictate the value of unimportant coordinates if they are linked via constraints.--> <is_free_to_satisfy_constraints>false</is_free_to_satisfy_constraints> </Coordinate> <Coordinate name="_coord_1"> <!--List of components that this component owns and serializes.--> <components /> <!--The value of this coordinate before any value has been set. Rotational coordinate value is in radians and Translational in meters.--> <default_value>0</default_value> <!--The speed value of this coordinate before any value has been set. Rotational coordinate value is in rad/s and Translational in m/s.--> <default_speed_value>0</default_speed_value> <!--The minimum and maximum values that the coordinate can range between. Rotational coordinate range in radians and Translational in meters.--> <range>-10 10</range> <!--Flag indicating whether or not the values of the coordinates should be limited to the range, above.--> <clamped>false</clamped> <!--Flag indicating whether or not the values of the coordinates should be constrained to the current (e.g. default) value, above.--> <locked>false</locked> <!--If specified, the coordinate can be prescribed by a function of time. It can be any OpenSim Function with valid second order derivatives.--> <prescribed_function /> <!--Flag indicating whether or not the values of the coordinates should be prescribed according to the function above. It is ignored if the no prescribed function is specified.--> <prescribed>false</prescribed> <!--Flag identifies whether or not this coordinate can change freely when posing the model to satisfy kinematic constraints. When true, the coordinate's initial or specified value is ignored when considering constraints. This allows values for important coordinates, which have this flag set to false, to dictate the value of unimportant coordinates if they are linked via constraints.--> <is_free_to_satisfy_constraints>false</is_free_to_satisfy_constraints> </Coordinate> <Coordinate name="_coord_2"> <!--List of components that this component owns and serializes.--> <components /> <!--The value of this coordinate before any value has been set. Rotational coordinate value is in radians and Translational in meters.--> <default_value>0</default_value> <!--The speed value of this coordinate before any value has been set. Rotational coordinate value is in rad/s and Translational in m/s.--> <default_speed_value>0</default_speed_value> <!--The minimum and maximum values that the coordinate can range between. Rotational coordinate range in radians and Translational in meters.--> <range>-10 10</range> <!--Flag indicating whether or not the values of the coordinates should be limited to the range, above.--> <clamped>false</clamped> <!--Flag indicating whether or not the values of the coordinates should be constrained to the current (e.g. default) value, above.--> <locked>false</locked> <!--If specified, the coordinate can be prescribed by a function of time. It can be any OpenSim Function with valid second order derivatives.--> <prescribed_function /> <!--Flag indicating whether or not the values of the coordinates should be prescribed according to the function above. It is ignored if the no prescribed function is specified.--> <prescribed>false</prescribed> <!--Flag identifies whether or not this coordinate can change freely when posing the model to satisfy kinematic constraints. When true, the coordinate's initial or specified value is ignored when considering constraints. This allows values for important coordinates, which have this flag set to false, to dictate the value of unimportant coordinates if they are linked via constraints.--> <is_free_to_satisfy_constraints>false</is_free_to_satisfy_constraints> </Coordinate> <Coordinate name="_coord_3"> <!--List of components that this component owns and serializes.--> <components /> <!--The value of this coordinate before any value has been set. Rotational coordinate value is in radians and Translational in meters.--> <default_value>0</default_value> <!--The speed value of this coordinate before any value has been set. Rotational coordinate value is in rad/s and Translational in m/s.--> <default_speed_value>0</default_speed_value> <!--The minimum and maximum values that the coordinate can range between. Rotational coordinate range in radians and Translational in meters.--> <range>-10 10</range> <!--Flag indicating whether or not the values of the coordinates should be limited to the range, above.--> <clamped>false</clamped> <!--Flag indicating whether or not the values of the coordinates should be constrained to the current (e.g. default) value, above.--> <locked>false</locked> <!--If specified, the coordinate can be prescribed by a function of time. It can be any OpenSim Function with valid second order derivatives.--> <prescribed_function /> <!--Flag indicating whether or not the values of the coordinates should be prescribed according to the function above. It is ignored if the no prescribed function is specified.--> <prescribed>false</prescribed> <!--Flag identifies whether or not this coordinate can change freely when posing the model to satisfy kinematic constraints. When true, the coordinate's initial or specified value is ignored when considering constraints. This allows values for important coordinates, which have this flag set to false, to dictate the value of unimportant coordinates if they are linked via constraints.--> <is_free_to_satisfy_constraints>false</is_free_to_satisfy_constraints> </Coordinate> </coordinates> <!--Physical offset frames owned by the Joint that are typically used to satisfy the owning Joint's parent and child frame connections (sockets). PhysicalOffsetFrames are often used to describe the fixed transformation from a Body's origin to another location of interest on the Body (e.g., the joint center). When the joint is deleted, so are the PhysicalOffsetFrame components in this list.--> <frames /> <!--Radii of the thoracic surface ellipsoid a Vec3(rX, rY, rZ).--> <thoracic_ellipsoid_radii_x_y_z>NaN NaN NaN</thoracic_ellipsoid_radii_x_y_z> <!--Winging axis origin (x,y coordinates) in the scapula plane (tangent to the thoracic surface).--> <scapula_winging_axis_origin>0 0</scapula_winging_axis_origin> <!--Winging axis orientation (in radians) in the scapula plane.--> <scapula_winging_axis_direction>0</scapula_winging_axis_direction> </ScapulothoracicJoint>
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值