简体   繁体   中英

hibernate does not set aliases and oracle throw `ORA-00918: column ambiguously defined` error

I'm using hibernate 4.3.11.Final, and have this model and .hbm.xml file:

Domain Class

public class Dossier {
    private Long    id;
    private Power   power;
    private String  dossierNumber;

    // getter and stters
}

Hibernate Mapping File

<hibernate-mapping>
    <class   name="com.rgh.Dossier"  table="DU_Dossier"  schema="DTR">
        <id name="id" type="long" >
            <column name="id"  />
            <generator class="sequence" >
                <param name="sequence">DTR.SEQ_Dossier</param>
            </generator>
        </id>

        <many-to-one    name="power"                column="power_Id"               entity-name="com.rgh.Power" not-null="true" />
        <property       name="dossierNumber"        column="dossier_Number"         type="string"       not-null="true" />

        <filter name="powerAuthorize" condition=" (exists (select t.id
                                                             from power_authorize t
                                                            where t.userid = :userId
                                                              and t.id = power_Id)

                                                    or (select count(*) from dossier_detail udd where udd.dossier_id = id) = 0
                                                   )"/>
    </class>
</hibernate-mapping>

and this is the method that i call in the repository:

@Autowired
private SessionFactory  sessionFactory;

public Session getSession() {
    try {
        return sessionFactory.getCurrentSession();
    }
    catch (Exception e) {
        return sessionFactory.openSession();
    }
}

@Override
public boolean changeStatus(Long dossierId, DossierStatus newStatus) {
    String hql =    " update " + Dossier.class.getName() + " e  " +
                    " set e.status = :newStatus                 " +
                    " where e.id = :dossierId                   ";

    Session session = getSession();
    Filter filter = session.enableFilter("powerAuthorize");
    filter.setParameter("userId", SecurityUtility.getAuthenticatedUser().getId());

    Query query = session.createQuery(hql);

    query.setParameter("newStatus", newStatus);
    query.setParameter("dossierId", dossierId);

    return query.executeUpdate() > 0;
}

and this the generated sql query when call method:

update DTR.DU_Dossier
   set status = ?
 where (exists
        (select t.id
           from power_authorize t
          where t.userid = ?
            and t.id = power_Id) and
        or (select count(*) from dossier_detail udd where udd.dossier_id = id) = 0)
   and ID = ?

and because hql does not set an aliase for DU_Dossier in update DTR.DU_Dossier segment of query, because i used id in select count(*) from dossier_detail udd where udd.dossier_id = id) = 0 segment of query i get this below error:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00918: column ambiguously defined

How can i solve this problem?

Try to define the filter expression without aliases.. just straight table names:

(exists (select power_authorize.id
              from power_authorize 
              where power_authorize.userid = :userId
                  and power_authorize.id = du_dossier.power_Id)
                  or (select count(*) from dossier_detail 
                        where dossier_detail.dossier_id = du_dossier.id) = 0
        )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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