编写插入HQL查询与多对一映射

时间:2014-06-02 19:37:40

标签: java hibernate java-ee jpa hql

我需要将以下SQL查询编写为HQL

insert into aab_appl_training tr  (tr.training_id,tr.appl_id,tr.course_name,tr.completed_dt,tr.cert_by,tr.cert_num,tr.train_comp)
select aab_appl_training_seq.nextval,'10071',tr.course_name,tr.completed_dt,tr.cert_by,tr.cert_num,tr.train_comp 
from aab_appl_training tr
where tr.appl_id=10018 

我的困境在于我的映射文件中我将AABApplication映射为aab_appl_training表的多对一

<hibernate-mapping>
<class name="org.sae.model.aab.AABTraining" table="AAB_APPL_TRAINING" schema="CMS">
    <id name="trainingId" type="java.lang.Long">
        <column name="TRAINING_ID" precision="10" scale="0" />
        <generator class="sequence">
        <param name="sequence">AAB_APPL_TRAINING_SEQ</param></generator>
    </id>
    <many-to-one name="AABApplication" class="org.sae.model.aab.AABApplication" fetch="select">
        <column name="APPL_ID" precision="10" scale="0" not-null="true" />
    </many-to-one>

我为此编写的HQL是

String hql="insert into AABTraining (tr.applId,tr.courseName,tr.completedDt,tr.certBy,tr.certNum,tr.trainComp)"+" "+"select :new_appl,tr.courseName,tr.completedDt,tr.certBy,tr.certNum,tr.trainComp"+" "+ 
"from AABTraining tr "+" "+"where tr.applId=:orig_appl" ;

然而,对于这个我得到了

的例外
[ERROR][2014-06-02 15:28:03,082] [] [AAB] [PARSER] []  <AST>:1:28: unexpected AST node: .[ERROR][2014-06-02 15:28:03,083] [] [AAB] [AABRepositoryImpl] []  org.hibernate.QueryException: could not resolve property:  of:org.sae.model.aab.AABTraining [insert into AABTraining (tr.applId,tr.courseName,tr.completedDt,tr.certBy,tr.certNum,tr.trainComp) select :new_appl,tr.courseName,tr.completedDt,tr.certBy,tr.certNum,tr.trainComp from org.sae.model.aab.AABTraining tr  where tr.applId=:orig_appl]
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)

请帮我写相同的正确查询。 感谢

1 个答案:

答案 0 :(得分:1)

您可以在HQL中使用INSERT/SELECT,但您的查询存在一些问题:

  1. 您不需要在插入列表中使用任何别名。在那里,您可以使用选择查询指定要设置的实体属性。 insert和select都应该在相同的列数上运行。
  2. &#34;选择:new_appl&#34;是非法的,因为参数仅在where子句中有效。
  3. 您可以保留选择查询的别名,但不要将其转移到插入查询。