0

我是 HQL 新手,想使用 INSERT stmt 将数据直接插入数据库。使用以下代码时: CompanyMaster 是模型名称(CompanyMaster.java

public void insertCompanyData(CompanyMaster company) {
    logger.info("Entering insertCompanyData");

    Session session = null;
    try{
        session = getSession();
        String hql = "INSERT INTO CompanyMaster (CompName,description,status) SELECT  (company.getCompName(),company.getDescription(),company.getStatus())";
        //String hql = "INSERT INTO CompanyMaster company(CompName,description,status) VALUES (company.getCompName(),company.getDescription(),company.getStatus())";
        Query query = session.createQuery(hql);
        //query.setString("groupId", groupId);
        query.executeUpdate();
    } finally{
        releaseSession(session);
    }

    logger.info("Exiting insertCompanyData");
}

我收到这样的错误

2013-07-23 09:34:53 INFO  [http-8080-2] CompanyDaoImpl.java:157 - Entering insertCompanyData
2013-07-23 09:34:53 ERROR [http-8080-2] ErrorCounter.java:56 - <AST>:0:0: unexpected end of subtree
2013-07-23 09:34:53 ERROR [http-8080-2] ErrorCounter.java:56 - <AST>:0:0: unexpected AST node: {vector}
2013-07-23 09:34:53 INFO  [http-8080-2] CompanyMasterServiceImpl.java:121 - Entering buildCompanyMasterDropdowns .. 
2013-07-23 09:34:53 INFO  [http-8080-2] CodesDaoImpl.java:27 - Entering findCodesByType()
Hibernate: select this_.CODE_MASTER_ID as CODE1_8_0_, this_.CR_TS as CR2_8_0_, this_.CR_USR as CR3_8_0_, this_.MD_TS as MD4_8_0_, this_.MD_USR as MD5_8_0_, this_.CODE_ID as CODE6_8_0_, this_.CODE_MASTER_TYPE as CODE7_8_0_, this_.CODE_DESC as CODE8_8_0_, this_.STATUS as STATUS8_0_ from FBMS.MST_CODE this_ where this_.CODE_MASTER_TYPE=? and this_.STATUS=? order by this_.CODE_DESC asc
2013-07-23 09:34:53 INFO  [http-8080-2] CodesDaoImpl.java:33 - Exiting findCodesByType() ..

请建议如何纠正此错误...

4

1 回答 1

0

查询的选择部分必须是有效的 HQL 选择查询,返回与插入子句中指定的列匹配的列。

如果要从内存中的值插入新行到表中,只需使用persist()

CompanyMaster c = new CompanyMaster();
c.setName(someName);
...
session.persist(c);
于 2013-07-23T06:54:41.760 回答