-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate picks wrong id
PostPosted: Tue Oct 25, 2011 11:36 am 
Newbie

Joined: Mon Aug 15, 2005 3:43 am
Posts: 15
I have two classes mapped like this

Code:
@Entity
public class Media implements Serializable {
@Column(name = "media_id")
    public long getId() {
        return id;
    }
@Id
    @Column(name = "release_id")
    public long getReleaseId() {
        return releaseId;
    }
...


Code:
package ca.cbc.medialib.entity;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;

import ca.cbc.medialib.entity.enums.ProcessStatus;

import java.util.Date;


@Entity
@Table(name = "processing_status")
public class ProcessingStatus {
    private long id;    //release id
    private ProcessStatus status;    //process status
    private Date startTime;

    public ProcessingStatus() {
    }

    public ProcessingStatus(long id, ProcessStatus status) {
        this.setId(id);
        this.setStatus(status);
        startTime = new Date();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProcessingStatus fm = (ProcessingStatus) o;
        if (this.getId() != fm.getId() || this.getStatus() != fm.getStatus())
            return false;

        return true;
    }

    @Id
    public long getId() {
        return id;
    }

    @Enumerated(EnumType.STRING)
    public ProcessStatus getStatus() {
        return status;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date t) {
        this.startTime = t;
    }


    @Override
    public int hashCode() {
        int result = 31 * (int) id;
        return result;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setStatus(ProcessStatus status) {
        this.status = status;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("ProcessStatus");
        sb.append("{releaseId=").append(id);
        sb.append(", status=").append(status);
        sb.append('}');
        return sb.toString();
    }
}



Now I try to get a temporary object which has information from these two objects:

Code:
select new MediaStatus(media,status) from Media media , ProcessingStatus status" +
                " where media.releaseId = status.id"

Now the hibernate issues the following query to fetch the ids of the related Media and Processing objects:
Code:
DEBUG 1774560059@qtp-788730640-0 org.hibernate.hql.ast.QueryTranslatorImpl - SQL: select media0_.media_id as col_0_0_, processing1_.id as col_1_0_ from Media media0_ cross join processing_status processing1_ where media0_.release_id=processing1_.id


Then it tries to fetch Media based on the the value for media_id, which fails as it is not the id

Code:

DEBUG 1774560059@qtp-788730640-0 org.hibernate.SQL -
    select
        media0_.release_id as release1_1_0_,
        media0_.account as account1_0_,
        media0_.ad_category as ad3_1_0_,
        media0_.air_date as air4_1_0_,
        media0_.audio_video as audio5_1_0_,
        media0_.available_date as available6_1_0_,
        media0_.available_in_mobile as available7_1_0_,
        media0_.chapter_start_times as chapter8_1_0_,
        media0_.countries as countries1_0_,
        media0_.delivery as delivery1_0_,
        media0_.description as descrip11_1_0_,
        media0_.episode_number as episode12_1_0_,
        media0_.exclude_countries as exclude13_1_0_,
        media0_.genre as genre1_0_,
        media0_.media_id as media15_1_0_,
        media0_.keywords as keywords1_0_,
        media0_.length as length1_0_,
        media0_.live_ondemand as live18_1_0_,
        media0_.expiry_date as expiry19_1_0_,
        media0_.yospace_id as yospace20_1_0_,
        media0_.original_filename as original21_1_0_,
        media0_.release_url as release22_1_0_,
        media0_.season_number as season23_1_0_,
        media0_.show_name as show24_1_0_,
        media0_.streaming_category_fulltitle as streaming25_1_0_,
        media0_.thumbnail_url as thumbnail26_1_0_,
        media0_.title as title1_0_
    from
        Media media0_
    where
        media0_.release_id=?


Why hibernate fetched media_id instead of release_id which is marked as @Id?


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

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.