0

I have an Entity shown as below, there are two tables mapped by annotation @SecondaryTable. The PrimaryKey's names of two tables are both 'resource_id'. When I execute HQL

delete AssetProjectProposalInfoEntity e where e.resourceId=:rid

Column ambiguously defined error occured.

import ...

@Entity
@Table(name = "ASSET_PROJECT_INFO", schema = "CORE_WAC")
@SecondaryTable(name="ASSET_PROJECT_PROPOSAL_INFO", schema="CORE_WAC")
public class AssetProjectProposalInfoEntity
{   
    @Id
    @Column(name = "RESOURCE_ID")
    @GeneratedValue(generator="UUIdentifier")
    @GenericGenerator(name="UUIdentifier", strategy = "uuid")
    private String resourceId;

    @Column(name = "PROJECT_NAME")
    private String projectName;


    @Column(table="ASSET_PROJECT_PROPOSAL_INFO", name="PROJECT_TYPE")
    private String projectType;


    @Column(table="ASSET_PROJECT_PROPOSAL_INFO", name="APPLY_UNIT_ID")
    private String applyUnitId;
    ...

}

The console log as below.It shows that the where clause in the SQL is RESOURCE_ID=? , but it should be assetproje0_.RESOURCE_ID=? I think. Can any body help? Thanks:)

Hibernate: insert into HT_ASSET_PROJECT_INFO select assetproje0_.RESOURCE_ID as 
RESOURCE_ID from CORE_WAC.ASSET_PROJECT_INFO assetproje0_ left outer join 
CORE_WAC.ASSET_PROJECT_PROPOSAL_INFO assetproje0_1_ on 
assetproje0_.RESOURCE_ID=assetproje0_1_.RESOURCE_ID where RESOURCE_ID=?

Hibernate: delete from HT_ASSET_PROJECT_INFO
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
Rong.l
  • 338
  • 2
  • 13
  • Are your table mappings the correct way round? If so it is a bit confusing: AssetProjectProposalInfoEntity is mapped to ASSET_PROJECT_INFO as primary table and ASSET_PROJECT_PROPOSAL_INFO as secondary??? – Alan Hay May 10 '14 at 10:01
  • @AlanHay Hi, I am newbie in hibernate. I have mapped two tables in one Entity by annotation approach using 'SecondaryTable'. ASSET_PROJECT_INFO table storages the common info through the whole application, and there are many other tables like ASSET_PROJECT_PROPOSAL_INFO table have association with ASSET_PROJECT_INFO table. So I use this way to map ASSET_PROJECT_INFO table with other tables. It means that I will have many Entities which created for every association between ASSET_PROJECT_INFO table and other tables. I am not sure that if it is correct doing so. – Rong.l May 10 '14 at 10:29
  • I finally took session.delete(entity) instead of using HQL – Rong.l May 13 '14 at 08:21

1 Answers1

0

Use two natives queries

    String deleteTable2 =
            "DELETE CORE_WAC.ASSET_PROJECT_PROPOSAL_INFO act where RESOURCE_ID IN (:resourceIdsList)";
    Query qDeleteTable2 = getEntityManager().createNativeQuery(deleteTable2);
    qDeleteTable2.setParameter("resourceIdsList", resourceIdsList);
    qDeleteTable2.executeUpdate();

    String deleteTable1 =
            "DELETE CORE_WAC.ASSET_PROJECT_INFO act where RESOURCE_ID IN (:resourceIdsList)";
    Query qDeleteTable1 = getEntityManager().createNativeQuery(deleteTable1);
    qDeleteTable1.setParameter("resourceIdsList", resourceIdsList);
    qDeleteTable1.executeUpdate();