7

I have a table ,say, Instrument with ID,State, and User_ID as columns.

So I have this JPA query to return all the instrument records with a matching User_ID.

   query = manager.createQuery("SELECT instrument from Instrument instrument
             where instrument.User_ID=:User_ID",Instrument.class);
   query.setParameter("User_ID", User_ID);

   List<Instrument> instruments=  query.getResultList();

   for(Instrument instrument:instruments){
            System.out.println("Instrument ID  "+instrument.getID());
              // using sysout as it is not prod code yet
        }

It is returning only the first record repeated as many times as there are matching records.

11:13:01,703 INFO  [stdout] (http-/127.0.0.1:8080-1) Instrument ID   1
11:13:01,704 INFO  [stdout] (http-/127.0.0.1:8080-1) Instrument ID   1
11:13:01,704 INFO  [stdout] (http-/127.0.0.1:8080-1) Instrument ID   1

I have three records in Db with instrument IDs 1,2, and 3

I enabled show sql query on hibernate and the query runs fine on the Database directly and returns distinct records.

Hibernate Query:

    select instrumentjdo0_.User_ID as member_U1_0_, instrumentjdo0_.ID as ID2_0_, 
instrumentjdo0_.state as state4_0_ from instrument instrumentjdo0_ where instrumentjdo0_.User_ID=?

Instrument Entity

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;




@Entity
@Table(name = "instrument")
public class Instrument{

    @Id
    @Column(name="User_ID", length=9, unique=true, nullable=false)
    String user_ID;

    @Column(name="ID",nullable=false)
    String ID;


    @Column(name="state",nullable=false)
    String state;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getUserID() {
        return user_ID;
    }

    public void setUserID(String userID) {
        this.user_ID = userID;
    }


    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

Not sure what I am missing.

9
  • looks like you have posted wrong Query in your question, check it once again --> User_ID=: "user_ID", <-- why you have double quotes here?
    – Learner
    Commented Sep 24, 2014 at 15:41
  • Can you add the query generated by hibernate to your question.
    – Learner
    Commented Sep 24, 2014 at 15:43
  • I have added the hibernate query too Commented Sep 24, 2014 at 15:52
  • ok, query looks good. Now how can you say that the query is returning same record multiple times? How you are iterating the list, can you please add that code also?
    – Learner
    Commented Sep 24, 2014 at 15:55
  • I have added the iteration code Commented Sep 24, 2014 at 16:00

4 Answers 4

18

The issue was that the wrong column in the Instrument Entity had the @ID attribute assigned to it.

I removed it from User_ID and Added it to ID and it worked fine.

5
  • But that will not result in repeated records while running your query right, so whatever records you have in DB has same values for User_ID?
    – Learner
    Commented Sep 24, 2014 at 17:17
  • yes all the records that are associated with the same user have the same User_ID. Commented Sep 24, 2014 at 17:31
  • oh ok, then this statement is misleading in your question -- I enabled show sql query on hibernate and the query runs fine on the Database directly and returns distinct records.
    – Learner
    Commented Sep 24, 2014 at 17:35
  • surprisingly that did return distinct records. I think hibernate does not directly return the result set and does some kind of processing over the results based on @ID etc. Commented Sep 24, 2014 at 17:47
  • Got the same issue but I did not have an alternate column on which to put the @Id thus I created a new column. Commented Jul 4, 2016 at 12:14
10

I also had faced same issue. For contact table i marked only firstname column as @Id. And table had multiple rows with same firstname due to which first row record with same firstname was getting duplicated in entire result set. To resolve this issue i made IdClass with first name and last name as id attribute and imported it as id class in my bean. Since firstname and lastname together form unique combination, it resolved my issue.

Idclass as below

public class ContactKey implements Serializable{

    protected String firstName;
    protected String lastName;

    public boolean equals(final Object inObject) {
        if (null != inObject) {
            if (inObject.getClass().equals(this.getClass())) {
                CqCamAdminKey siteKey = (CqCamAdminKey) inObject;
                return (null != this.getFirstName() && this.getFirstName().equals(siteKey.getFirstName()) && null != this.getLastName() && this.getLastName().equals(siteKey.getLastName()));
            }
        }
        return super.equals(inObject);
    }

    public int hashCode() {
        if (this.getFirstName() != null && this.getLastName() != null) {
            return this.getFirstName().hashCode() + this.getLastName().hashCode();
        }
        return super.hashCode();
    }
}

bean class as below

@IdClass(contactKey.class)
public abstract class CqCamAdminDataBean implements DataModelConstants{

    private static final long serialVersionUID = 7686374823515894764L;

    @Id
    @JsonIgnore
    @XmlTransient
    @Column(name = FIRST_NAME)
    protected String firstName;


    @Id
    @JsonIgnore
    @Column(name = LAST_NAME)
    protected String lastName;

}
1
2

Issue with @Id column, If we check closely, @Id column value is same for all the rows. Hence hibernate/JPA not able to get different records, it just get 1st record with this @Id and return duplicate records of it.

Solution - Use @IdClass with columns which result in unique row instead of duplicate row.

1

Make sure to specify DISTINCT in your JPQL query: "SELECT DISTINCT instrument from Instrument instrument where instrument.User_ID=:User_ID". For scalar queries, DISTINCT essentially removes underlying duplicates from the result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.