SteeleDev
SteeleDev

Reputation: 169

Writing Insert HQL Query with Many-to-One mapping

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

<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

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

Upvotes: 1

Views: 975

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153690

You can use INSERT/SELECT in HQL but your query has some issues:

  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.
  3. You can keep the alias for the select query but don;t transfer it to the insert one.

Upvotes: 1

Related Questions