2011-08-04 126 views
-2

我們有Web應用程序Extjs,Spring3.0,JPA,Hibernate。我有單一的實體工作正常,但昨天我已經添加@manytoone在用戶實體和應用程序停止工作的搜索頁面。 Extjs開始拋出異常。當我嘗試調試時發現@manytoOne關係存在問題。任何人都可以給我提示嗎?Extjs + Spring + JPA + HIbernate

@Controller 
public class BusinessCalsController { 

     @Autowired 
     private BusinessCalService businessCalService; 

     public void setBusinessCalService(BusinessCalService businessCalService) { 
      this.businessCalService = businessCalService; 
     } 

     @SuppressWarnings("unchecked") 
     @RequestMapping(value="/businessCalView.do",method=RequestMethod.GET) 
     public @ResponseBody Map<String,? extends Object> view(HttpServletRequest request, HttpServletResponse response) throws Exception { 
      try{ 
       String strStart = request.getParameter("start"); //start index 
       String strLimit = request.getParameter("limit"); //limit record num 
       String strSort = request.getParameter("sort"); //desc or asc 
       String strDir = request.getParameter("dir"); //property for sort 
       Map businessCal = (Map)businessCalService.getBusinessCalList(strStart, strLimit, strSort, strDir); 
       Map modelMap = getJSONMap((List<BusinessCal>) businessCal.get("resultSet")); 
       return modelMap; 
      } catch (Exception e) { 
       return getModelMapError("Error retrieving Machine from database."); 
      } 
      } 


     /** 
     * Generates modelMap to return in the modelAndView 
     * @param contacts 
     * @return 
     */ 
     private Map<String,Object> getJSONMap(List<BusinessCal> businessCal){ 

      Map<String,Object> modelMap = new HashMap<String,Object>(3); 
      modelMap.put("total", businessCal.size()); 
      modelMap.put("data", businessCal); 
      modelMap.put("success", true); 
      return modelMap; 
     } 

     /** 
     * Generates modelMap to return in the modelAndView in case 
     * of exception 
     * @param msg message 
     * @return 
     */ 
     private Map<String,Object> getModelMapError(String msg){ 

      Map<String,Object> modelMap = new HashMap<String,Object>(2); 
      modelMap.put("message", msg); 
      modelMap.put("success", false); 
      return modelMap; 
     } 
} 

@Service 
public class BusinessCalService { 
    @Autowired 
    private BusinessCalDAO businessCalDAO; 
    private BusinessCalJsonUtil util; 

    /** 
    * Get all contacts 
    * @return 
    */ 
    @Transactional(readOnly=true) 
    public Object getBusinessCalList(String strStart,String strLimit,String strSort, String strDir){ 
     return businessCalDAO.searchBusinessCal(strStart, strLimit, strSort, strDir); 
    } 

    /** 
    * @return the userCalDAO 
    */ 
    public BusinessCalDAO getBusinessCalDAO() { 
     return businessCalDAO; 
    } 

    /** 
    * @param userCalDAO the userCalDAO to set 
    */ 
    @Autowired 
    public void setBusinessCalDAO(BusinessCalDAO businessCalDAO) { 
     this.businessCalDAO = businessCalDAO; 
    } 

    /** 
    * Spring use - DI 
    * @param util 
    */ 
    @Autowired 
    public void setJsonUtil(BusinessCalJsonUtil util) { 
     this.util = util; 
    } 
} 

@Repository 
public class BusinessCalDAO { 

    @Autowired 
    private HibernateTemplate hibernateTemplate; 
    private boolean flag; 

    /** 
    * @return the hibernateTemplate 
    */ 
    @SuppressWarnings("unchecked") 
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate){ 
     this.hibernateTemplate = hibernateTemplate; 
    } 

    /** 
    * Get List of UsersCals from database 
    * @return list of all contacts 
    */ 
    @SuppressWarnings("unchecked") 
    public Object searchBusinessCal(String strStart,String strLimit,String strSort, String strDir) { 
     PageHibernate newPage = null; 
     BusinessCal obj = new BusinessCal(); 
     newPage = new PageHibernate("from BusinessCal", (strStart != null && !strStart.equals("null")) ? Integer.parseInt(strStart) : 0 , (strLimit != null && !strLimit.equals("null")) ? Integer.parseInt(strLimit) : 0 ,obj.getClass()); 
     return hibernateTemplate.execute(newPage); 
    } 
} 


@SuppressWarnings("serial") 
@Entity 
@Table(name="SP_BU_CALS",schema ="TAX_STG") 
public class BusinessCal implements Serializable{ 

    private BULookUp objBULookUp; 
    private Integer intID; 
    private String strBussUnitId; 

    /** 
    * @return the intID 
    */ 
    @Id 
    @Column(name="ID") 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "FEED_SEQ") 
    @SequenceGenerator(name="FEED_SEQ", sequenceName = "TAX_FEED.FEED_SEQ")  
    public Integer getIntID() { 
     return intID; 
    } 
    /** 
    * @param intID the intID to set 
    */ 
    public void setIntID(Integer intID) { 
     this.intID = intID; 
    } 
    /** 
    * @return the strBusinessUnitId 
    */ 
    @Column(name="BU_ID") 
    public String getStrBussUnitId() { 
     return strBussUnitId; 
    } 
    /** 
    * @param strBusinessUnitId the strBusinessUnitId to set 
    */ 
    public void setStrBussUnitId(String strBussUnitId) { 
     this.strBussUnitId = strBussUnitId; 
    } 

    /** 
    * @return the objBULookUp 
    */ 
    @ManyToOne 
    @JoinColumn(name = "BU_ID", insertable = false, updatable = false) 
    public BULookUp getObjBULookUp() { 
     return objBULookUp; 
    } 
    /** 
    * @param objBULookUp the objBULookUp to set 
    */ 
    public void setObjBULookUp(BULookUp objBULookUp) { 
     this.objBULookUp = objBULookUp; 
    } 
} 

問題開始arice時,我已經在BusinessCal模型中添加BULookUp objBULookUp。 讓我添加BULookUp的代碼以及extjs文件的代碼。

@SuppressWarnings("serial") 
@Entity 
@Table(name="SP_BU_LKUP",schema ="TAX_STG") 
public class BULookUp implements Serializable{ 

    private String strBusinessUnitID; 
    private String strBusinessUnitDescription; 
    private String strCBDCode; 
    private Integer intActive; 

    /** 
    * @return the strBusinessUnitID 
    */ 
    @Id 
    @Column(name="BU_ID") 
    public String getStrBusinessUnitID() { 
     return strBusinessUnitID; 
    } 
    /** 
    * @param strBusinessUnitID the strBusinessUnitID to set 
    */ 
    public void setStrBusinessUnitID(String strBusinessUnitID) { 
     this.strBusinessUnitID = strBusinessUnitID; 
    } 
    /** 
    * @return the strBusinessUnitDescription 
    */ 
    @Column(name="BU_DESCR") 
    public String getStrBusinessUnitDescription() { 
     return strBusinessUnitDescription; 
    } 
    /** 
    * @param strBusinessUnitDescription the strBusinessUnitDescription to set 
    */ 
    public void setStrBusinessUnitDescription(String strBusinessUnitDescription) { 
     this.strBusinessUnitDescription = strBusinessUnitDescription; 
    } 
    /** 
    * @return the strCBDCode 
    */ 
    @Column(name="CBD") 
    public String getStrCBDCode() { 
     return strCBDCode; 
    } 
    /** 
    * @param strCBDCode the strCBDCode to set 
    */ 
    public void setStrCBDCode(String strCBDCode) { 
     this.strCBDCode = strCBDCode; 
    } 
    /** 
    * @return the intActive 
    */ 
    @Column(name="ACTIVE") 
    public Integer getIntActive() { 
     return intActive; 
    } 
    /** 
    * @param intActive the intActive to set 
    */ 
    public void setIntActive(Integer intActive) { 
     this.intActive = intActive; 
    } 
} 

請看以下代碼ExtJS的

app = function() { 

     var createForm; 
     var updateForm; 
     var ds; 
     var dataGrid; 
     var menu; 
     var updateMenuItem; 
     var createMenuItem; 
     var readMenuItem; 
     var deleteMenuItem; 
     var tb; 
     var sm; 
     var userRecord; 
     var reader; 
     var writer; 
     var proxy; 
     var pagetbUsers; 

     var getContext = function() { 

      var base = document.getElementsByTagName('base')[0]; 
      if (base && base.href && (base.href.length > 0)) { 
       base = base.href; 
      } else { 
       base = document.URL; 
      } 
      return base.substr(0, base.indexOf("/", base.indexOf("/", base.indexOf("//") + 2) + 1)); 
     }; 

     var onRowClick = function(SelectionModel,rowIndex,record) { 
      onUpdateClick() 
     }  
     return { 
      init:function() { 

      businessCalRecord = Ext.data.Record.create([ 
        {name: 'intID', type: 'int'}, 
        {name: 'strBussUnitId', type: 'string'}, 
        {name: 'intCalCount', type: 'int'}, 
        {name: 'strCalType',type: 'string'}, 
        {name: 'strCalDescription', type: 'string'}, 
        {name: 'strCountType', type: 'string'}, 
        {name: 'strCalStart', type: 'string'}, 
        {name: 'strCalEnd', type: 'string'}, 
        {name: 'strCreatedBy', type: 'string'}, 
        {name: 'tmpCreatedDate', type: 'date'}, 
        {name: 'strActive', type: 'string'}, 
        {name: 'tmpInActiveDate', type: 'string'}, 
        {name: 'strCalID', type: 'string'}, 
        {name: 'objBULookUp.strBusinessUnitID', type: 'string'} 
       ]); 

      // The new DataWriter component. 
      writer = new Ext.data.JsonWriter({ 
       encode: true, 
       writeAllFields: true 
      }); 

      // The new DataReader component. 
      reader = new Ext.data.JsonReader({ 
       totalProperty: 'total', 
       successProperty: 'success', 
       idProperty: 'intID', 
       root: 'data', 
       messageProperty: 'message' // <-- New "messageProperty" meta-data 
      }, 
      businessCalRecord); 

      proxy = new Ext.data.HttpProxy(new Ext.data.Connection({ 
       api: {read: {url:'businessCalView.do', method: 'GET'}}, 
       autoAbort: true 
      })); 

      proxy.addListener('exception', function(proxy, options, response, error, res) { 
       alert(error); 
       alert("proxy.addListener"); 
       Ext.Msg.show({ 
        title: 'ERROR', 
        msg: res.message, 
        icon: Ext.MessageBox.ERROR, 
        buttons: Ext.Msg.OK 
       }); 
      }); 

      proxy.on({ 
       "loadexception": function(proxy, options, response, error) { 
        alert(response.responseText); 
        alert("proxy.on"); 
        alert(response.status); 
        alert(response.message); 
       } 
      }); 

      // Typical Store collecting the Proxy, Reader and Writer together. 
      ds = new Ext.data.Store({ 
       proxy: proxy, 
       reader: reader, 
       writer: writer, // <-- plug a DataWriter into the store just as you would a Reader 
       autoSave: false // <-- false would delay executing create, update, destroy requests until specifically told to do so with some [save] buton. 
      }); 

      //read the data from simple array 
      //ds.load(); 
      ds.load({ params: { start: 0, limit: 5} }); 

      var cm = new Ext.grid.ColumnModel([ 
       {header: "id", dataIndex: 'intID',hidden: true}, 
       {header: "Bussiness Unit Id",dataIndex: 'objBULookUp.strBusinessUnitDescription',sortable: true}, 
       {header: "Cal Count", width:80, dataIndex: 'intCalCount', sortable: true}, 
       {header: "Cal Type", width: 100, dataIndex: 'strCalType', sortable: true}, 
       {header: "Cal Description", width: 180, dataIndex: 'strCalDescription', sortable: true}, 
       {header: "Count Type", width: 100, dataIndex: 'strCountType', sortable: true}, 
       {header: "Cal Start", width: 100, dataIndex: 'strCalStart', sortable: true}, 
       {header: "Cal End", width: 100, dataIndex: 'strCalEnd', sortable: true}, 
       {header: "Cal ID", width: 100, dataIndex: 'strCalID', sortable: true} 
      ]); 

      sm = new Ext.grid.RowSelectionModel({singleSelect:'true'}); 
      sm.addListener('rowselect',onRowClick,this); 

      var PagingBar = new Ext.PagingToolbar({ 
       pageSize: 5, 
       store: ds, 
       displayInfo: true, 
       displayMsg: 'Displaying topics {0} - {1} of {2}', 
       emptyMsg: 'No insurers to display' 
      }); 

      dataGrid = new Ext.grid.GridPanel({applyTo:'readPanel', frame: true, 
       ds:ds, 
       cm:cm, 
       sm:sm, 
       bbar: PagingBar, 
       title:'Search Page of Business Cal',  
       width: 910, 
       height: 195 
      });   

     }; 
}(); 

但是,當我刪除@ManyToOne相同的代碼工作正常。

欣賞你的努力,看看我的問題。

變量strBussUnitId是BusinessCal模型中屬於BULookUp模型的列。 BULookUp模型僅僅是主數據表,用於存儲通過strBussUnitId使用該主表數據的BusinessCal。

Noow在我們使用extjs的前端我無法顯示strBussUnitId,這是我需要爲strBussUnitId設置相應的strBusinessUnitDescription的數字。爲此,我使用BULookUp模型在BusinessCal模型中創建了@manytoOne。

當我在BusinessCal模型中添加@manytoOne時出現問題。

+2

發佈您的代碼和堆棧跟蹤或您遇到的任何錯誤。 – Rohit

+2

在任何人能夠幫助你之前,你需要提供更多的細節。向我們展示您的實體類(使用JPA註釋)並啓動異常堆棧跟蹤。 – ChssPly76

回答

0

不知道你得到了什麼異常,或者它來自哪裏,我想你會遇到與用於延遲初始化的Hibernate代理有關的問題。要麼你得到了LazyInitializationExceptions,爲此你應該應用open session in view pattern,否則你將編組Hibernate代理髮送回瀏覽器是有問題的,因爲它們不是由你的框架編組的,在這種情況下你需要以某種方式從對象圖中刪除代理,也許使用某種類型的彙編層。

相關問題