import java.util.List;
import java.util.Map;import org.hibernate.Session;
import org.hibernate.Transaction;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;import dao.Company;
import dao.Student;
import dao.StudentDAO;
@SuppressWarnings("unchecked")
public class StaffItemAction extends ActionSupport {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private List companystaff;
private Company company;
private StudentDAO studentdao;
public List getCompanystaff() {
return companystaff;
}
public void setCompanystaff(List companystaff) {
this.companystaff = companystaff;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public StudentDAO getStudentdao() {
return studentdao;
}
public void setStudentdao(StudentDAO studentdao) {
this.studentdao = studentdao;
}
public String execute() throws Exception
{
Map map=ActionContext.getContext().getSession();
Student student=(Student)map.get("student");
Company company=student.getCompany();
List students=null;
Session firstSession=studentdao.getSessionFactory().openSession();
Transaction tx=null;
Company companytemp=null;
try{
tx=firstSession.beginTransaction();
companytemp=(Company)firstSession.load(Company.class, company.getCompanyid());
students=studentdao.findByProperty("company", companytemp);
tx.commit();
}
catch(Exception e){
if(tx!=null)
tx.rollback();
throw e;
}
finally{
firstSession.close();
}
if(students.size()==0)
{
this.setCompany(companytemp);
return "success";
}
else
{
this.setCompany(companytemp);
this.setCompanystaff(students);
return "success";
}
}}
在action里用set方法设置了一个List的值,存放的是数据库查询的结果Student类型的对象
然后在页面上迭代输出companystaff的值,但是这有个问题,由于Student类型里有个属性是City类型的(hibernate:外键生成)感觉是因为实际上未加载实际对象,所以在迭代中依次输出每个Student的各个属性的时候,City这个属性不会显示,不知如何解决。我的显示代码片段为:
<s:iterator id="students" value="companystaff" status="st">
      <tr >
       <td colspan="2" bgcolor="#ff80ff">
        <s:property value="username"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
        <s:property value="college"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
        <s:property value="cityByTownId.cityname"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">&nbsp;
        <s:property value="major"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
         <s:property value="position"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
         <s:property value="sex"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
         <s:property value="phone"/>
       </td>
       <td colspan="2" bgcolor="#ff80ff">
         <s:property value="email"/>
       </td>
      </tr>
    </s:iterator>

解决方案 »

  1.   

    让hibernate级联操作不就搞定了
      

  2.   

    在取得students后在取得cityByTownId对象。例:
    1.对象定义
    public class TrnRelation implements java.io.Serializable {
        private Integer relationId;
        private TrnAddressRecord trnAddressRecord;  //对象属性
    }2.取值
    public TrnRelation findByRelationId(Integer relationId) {
    TrnRelation trnRelation = trnRelationDAO.findByRelationId(relationId);
    if (null != trnRelation) {
    if (trnRelation.getTrnAddressRecord() != null) {
    trnRelation.getTrnAddressRecord().getSyncKbnCd();
    }
    }

    return trnRelation;
    }
      

  3.   

    在取得students后在取得cityByTownId对象。 例: 
    1.对象定义 
    public class TrnRelation implements java.io.Serializable { 
        private Integer relationId; 
        private TrnAddressRecord trnAddressRecord;  //对象属性 
    } 2.取值 
    public TrnRelation findByRelationId(Integer relationId) { 
    TrnRelation trnRelation = trnRelationDAO.findByRelationId(relationId); 
    if (null != trnRelation) { 

      

  4.   

    把你的HIBERNATE里的级联操作的延迟加载设成FALSE试试