User --> Departmentmany - one如果User有department属性,而且你的配置文件里lazy=false可以直接得到Department
public User findByKey(String userId) {
return (User)getSession().load(User.class,userId);    
}public Department getUserDepartment(User user) {
return user.getDepartment();
|

解决方案 »

  1.   

    如上所述,奇怪的是我可以得到user.getDepartment().getDepartmentId()的值(通过departmentId关联的),取user.getDepartment().getDepartmentName()的值的时候,报错:
    org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed我的User.hbm.xml片段如下:  <class name="User" table="_User">
        <id name="userId" column="userId" type="java.lang.String">
          <generator class="assigned"/>
        </id>
        <property name="username" column="username" type="java.lang.String" not-null="true"/>
        <property name="password" column="password" type="java.lang.String" not-null="true"/>
       <many-to-one
          name="department"
          column="departmentId"
          class="Department"
          cascade="save-update"
          outer-join="auto"
          not-null="true"
          lazy="false"
        />
      </class>
      

  2.   

    我理解的是他返回一个object[]的list,一般都是顺着取
      

  3.   

    用join Department时,返回的应该是Object[],
    这种关联的应用应该很多呀,本人是初学者,请大家指教。该怎么取关联表的数据。