简体   繁体   English

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

[英]Writing Insert HQL Query with Many-to-One mapping

I need to write the following SQL query as 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 

My Dilemma here is that in my Mapping files I have AABApplication mapped as a many to one to the aab_appl_training table 我的困境是,在我的映射文件中,我将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>

The HQL I have written for the same is 我为此写的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" ;

However for this I am getting an Exception of 但是为此,我得到一个例外

[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)

Do help me in writing the correct query for the same. 请帮助我编写相同的正确查询。 Thanks 谢谢

You can use INSERT/SELECT in HQL but your query has some issues: 您可以在HQL中使用INSERT / SELECT ,但是查询存在一些问题:

  1. You don't need to use any alias in the insert into list. 您无需在插入列表中使用任何别名。 There you specify the entity properties you are going to set with your select query. 在此处,您可以指定要通过选择查询设置的实体属性。 Both the insert and the select should operate on the same number of columns. 插入和选择操作都应在相同数量的列上进行。
  2. The "select :new_appl" is illegal since parameters are only valid in the where clause. “ select:new_appl”是非法的,因为参数仅在where子句中有效。
  3. You can keep the alias for the select query but don;t transfer it to the insert one. 您可以保留选择查询的别名,但不要将其转移到插入查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM