从表中选择记录并在另一个表中插入所选记录时,HQL插入失败?

时间:2013-03-21 05:35:12

标签: hibernate struts2 jpa-2.0

我的HQL插入查询在从表中选择记录并在另一个表中插入所选记录时失败。我正在获取所有debuging消息“内部列表”并在控制台中迭代列表但无法在公司表中插入记录。

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();

}

2 个答案:

答案 0 :(得分:1)

为什么要使用insert插入....

尝试创建Company company = new Company();

然后将所有值设置为此对象,然后使用.save();

进行保存

答案 1 :(得分: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();