These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Query parameters have swapped places from HQL to SQL
PostPosted: Thu Aug 16, 2007 8:12 am 
Newbie

Joined: Thu Aug 16, 2007 7:20 am
Posts: 2
Location: London
In the TRACE output below my HQL query has selection criteria which includes "campaign.id.campaignId in (set...)". In the DEBUG output 2 log entries later my the SQL selection criteria has changed to "campaignof0_.scheme_no in (set...)".

I can't understand why hibernate is mapping the wrong entity field in this way, or figure out a workaround. Has anyone seen this issue before?

Hibernate EntityManager 3.2.1.GA
Hibernate Annotations 3.2.1.GA
Hibernate 3.2.3

Backing database is Oracle 10g

I haven't included the code at this stage - still creating a test case.

2007-08-16 12:12:43,960 TRACE [org.hibernate.engine.query.HQLQueryPlan] find: FROM CampaignOffers WHERE status=?2 AND expires > CURRENT_TIMESTAMP AND campaign.id.campaignId in ('CAMPAIGN_1','CAMPAIGN_2','CAMPAIGN_3','CAMPAIGN_4','CAMPAIGN_5','CAMPAIGN_6') AND campaign.id.schemeNo=?1

2007-08-16 12:12:43,960 TRACE [org.hibernate.engine.QueryParameters] named parameters: {2=OPEN, 1=1}

2007-08-16 12:12:43,960 DEBUG [org.hibernate.SQL] select campaignof0_.offer_no as offer1_33_, campaignof0_.short_description as short2_33_, campaignof0_.expires as expires33_, campaignof0_.scheme_no as scheme4_33_, campaignof0_.created_date as created5_33_, campaignof0_.remaining_redemptions as remaining6_33_, campaignof0_.reduction_value as reduction7_33_, campaignof0_.min_award as min8_33_, campaignof0_.campaign_id as campaign29_33_, campaignof0_.offer_type as offer9_33_, campaignof0_.reduction_type as reduction28_33_, campaignof0_.created_by as created10_33_, campaignof0_.modified_by as modified11_33_, campaignof0_.modified_date as modified12_33_, campaignof0_.offer_seq_id as offer13_33_, campaignof0_.long_description as long14_33_, campaignof0_.status as status33_, campaignof0_.min_item as min16_33_, campaignof0_.min_value as min17_33_, campaignof0_.max_award as max18_33_, campaignof0_.budget as budget33_, campaignof0_.max_trans_redemptions as max20_33_, campaignof0_.max_redemptions as max21_33_, campaignof0_.max_customers as max22_33_, campaignof0_.remaining_budget as remaining23_33_, campaignof0_.remaining_customers as remaining24_33_, campaignof0_.alert_threshold_budget as alert25_33_, campaignof0_.alert_threshold_redemptions as alert26_33_, campaignof0_.alert_threshold_customer as alert27_33_ from dboppt.campaign_offers campaignof0_ where status=? and campaignof0_.expires>CURRENT_TIMESTAMP and (campaignof0_.scheme_no in ('CAMPAIGN_1' , 'CAMPAIGN_2' , 'CAMPAIGN_3' , 'CAMPAIGN_4' , 'CAMPAIGN_5' , 'CAMPAIGN_6')) and campaignof0_.campaign_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 9:34 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I couldn't get your query to execute as is. Needed to add the alias "campaign" because it was generating illegal arg exception "Unable to resolve path [campaign.id].

However, once I added the alias the query seemed to be ok (with hib/anno/em versions equal to yours).

Code:
14:29:41,000 DEBUG HQLQueryPlan:150 - find: FROM CampaignOffers campaign WHERE status=?2 AND expires > CURRENT_TIMESTAMP AND campaign.id.campaignId in ('CAMPAIGN_1','CAMPAIGN_2','CAMPAIGN_3','CAMPAIGN_4','CAMPAIGN_5','CAMPAIGN_6') AND campaign.id.schemeNo=?1
14:29:41,000 DEBUG QueryParameters:277 - named parameters: {2=OPEN, 1=1}

Hibernate: select campaignof0_.campaign_id as campaign1_0_, campaignof0_.scheme_no as scheme2_0_, campaignof0_.expires as expires0_, campaignof0_.status as status0_ from CampaignOffers campaignof0_ where campaignof0_.status=? and campaignof0_.expires>CURRENT_TIMESTAMP and (campaignof0_.campaign_id in ('CAMPAIGN_1' , 'CAMPAIGN_2' , 'CAMPAIGN_3' , 'CAMPAIGN_4' , 'CAMPAIGN_5' , 'CAMPAIGN_6')) and campaignof0_.scheme_no=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 9:46 am 
Newbie

Joined: Thu Aug 16, 2007 7:20 am
Posts: 2
Location: London
I think I found a workaround for this. I'm not sure if this is expected behaviour or not, so I 'd still like to see some feedback from the community :-)

I changed the query to simply return a list of entities. This failed when hibernate was hydrating the entity at the 4th or 5th field, which just happened to be a field in an embedded id.

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "campaign_id", unique = false, nullable = false, insertable = true, updatable = true),
@JoinColumn(name = "scheme_no", unique = false, nullable = false, insertable = true, updatable = true)
})
public Campaigns getCampaign() {
return this.campaign;
}

I checked my Campaigns class and noticed its Id field was declared with schemeNo, CampaignId:

@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "schemeNo", column = @Column(name = "scheme_no", unique = false, nullable = false, insertable = true, updatable = true)),
@AttributeOverride(name = "campaignId", column = @Column(name = "campaign_id", unique = false, nullable = false, insertable = true, updatable = true, length = 25)) })
public CampaignsId getId() {
return this.id;
}

Just on the off-chance I changed ordering or the columns in the @ManyToOne above to be schemeNo, campaignId, like in the Campaigns.Id.

Imagine my surprise when it suddenly worked!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 10:39 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Found this in the annotations documentation:
Quote:
It is considered a good practice to express referencedColumnNames explicitly. Otherwise, Hibernate will suppose that you use the same order of columns as in the primary key declaration.
Code:
    @ManyToOne
    @JoinColumns ({
        @JoinColumn(name="parentCivility", referencedColumnName = "isMale"),
        @JoinColumn(name="parentLastName", referencedColumnName = "lastName"),
        @JoinColumn(name="parentFirstName", referencedColumnName = "firstName")
    })
    public Parent parent; //unidirectional


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.