2013-03-21 174 views
0

我的HQL插入查詢在從表中選擇記錄並將選定記錄插入另一個表時失敗。我得到所有調試消息「內部列表」並在控制檯中迭代列表,但無法在公司表中插入記錄。從表中選擇記錄並將選定的記錄插入另一個表中時,HQL插入失敗?

DAO類代碼: -

public void approveCompany(int companyId, String varifier){ 
    session.beginTransaction(); 
    Query query=session.createQuery("from Provisional where companyID=?"); 
    query.setInteger(0, companyId); 
    @SuppressWarnings("unchecked") 
    List<Provisional> list=query.list();   
    session.getTransaction().commit(); 
if(list.size()>0) 
    { 
     System.out.println("inside list"); 
     for(Provisional p:list){ 
      System.out.println("iterating list"); 
Query approveQuery=session.createQuery("insert into Company(companyID" + 
        ",annualValueOfTotalExport,annualValueOfTotalSale,cityID,companyAddress" + 
        "companyEmail,companyName,companyType,contactPersionPosition,contactPersonEmail," + 
        "contactPersonFaxNo,contactPersonName,contactPersonPhoneNo,countryID,faxNo," + 
        "licenceNo,loginID,oldPassword,password,phoneNo,stateID,texID,updated_At," + 
        "verifierID,website,zipCode) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?," + 
        "?,?,?,?,?,?,?,?,?,?,?,?)"); 
      approveQuery.setParameter(1, p.getCompanyID()); 
      approveQuery.setParameter(2, p.getAnnualValueOfTotalExport()); 
      approveQuery.setParameter(3, p.getAnnualValueOfTotalSale()); 
      approveQuery.setParameter(4, p.getCityID()); 
      approveQuery.setParameter(5, p.getCompanyAddress()); 
      approveQuery.setParameter(6, p.getCompanyEmail()); 
      approveQuery.setParameter(7, p.getCompanyName()); 
      approveQuery.setParameter(8, p.getCompanyType()); 
      approveQuery.setParameter(9, p.getContactPersonPosition()); 
      approveQuery.setParameter(10, p.getContactPersonEmail()); 
      approveQuery.setParameter(11, p.getContactPersonFaxNo()); 
      approveQuery.setParameter(12, p.getContactPersonName()); 
      approveQuery.setParameter(13, p.getContactPersonPhoneNo()); 
      approveQuery.setParameter(14, p.getCountryID()); 
      approveQuery.setParameter(15, p.getFaxNo()); 
      approveQuery.setParameter(16, p.getLicenceNo()); 
      approveQuery.setParameter(17, p.getLoginID()); 
      approveQuery.setParameter(18, p.getPassword()); 
      approveQuery.setParameter(19, p.getPassword()); 
      approveQuery.setParameter(20, p.getPhoneNo()); 
      approveQuery.setParameter(21, p.getStateID()); 
      approveQuery.setParameter(22, p.getTaxID()); 
      approveQuery.setParameter(23, p.getCreated_On()); 
      approveQuery.setParameter(24, varifier); 
      approveQuery.setParameter(25, p.getWebsite()); 
         approveQuery.setParameter(26,p.getZipCode());   
      approveQuery.executeUpdate(); 
      session.getTransaction.commit(); 
     } 
    } 
    session.close(); 

} 
+0

什麼是你得到 – PSR 2013-03-21 05:36:59

+0

可以u顯示 – PSR 2013-03-21 05:41:26

+0

我不是在我的控制檯得到任何的異常異常 – 2013-03-21 05:48:42

回答

1

爲什麼你正在使用INSERT INTO ....

嘗試創建Company company = new Company();

然後所有的值設置爲這個對象,然後插入保存使用.save();

0

使用像這樣的批量插入。

hibernate.jdbc.batch_size 20注意

 Session session = sessionFactory.openSession(); 
    Transaction tx = session.beginTransaction(); 
    for (int i=0; i<100000; i++) 
     { 
      Customer customer = new Customer(.....); 
      session.save(customer); 
     if (i % 20 == 0) { //20, same as the JDBC batch size 
      //flush a batch of inserts and release memory: 
      session.flush(); 
      session.clear(); 
     } 
    } 

    tx.commit(); 
    session.close(); 
相關問題