jpa muli row select result is same row repeat

Java 자바 Jpa에서 멀티 로우 반환시 같은값이 반복 

JPA/Hibernate select query returning duplicate records

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.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

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.

  •  
    looks like you have posted wrong Query in your question, check it once again --> User_ID=: "user_ID", <-- why you have double quotes here? – Chaitanya Sep 24 '14 at 15:41
  •  
    yes you are right..fixed it! thanks! – DntFrgtDSemiCln Sep 24 '14 at 15:42
  •  
    Can you add the query generated by hibernate to your question. – Chaitanya Sep 24 '14 at 15:43
  •  
    I have added the hibernate query too – DntFrgtDSemiCln Sep 24 '14 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? – Chaitanya Sep 24 '14 at 15:55
     
     

    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.

[출처] https://stackoverflow.com/questions/26021050/jpa-hibernate-select-query-returning-duplicate-records

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
62 [maven 설정] Jackson Databind » 2.6.7.1 졸리운_곰 2020.10.05 187
61 [maven 설정] MongoDB Java Driver » 3.12.6 졸리운_곰 2020.10.05 175
60 Maven 에 직접 jar 라이브러리 추가하기!!! ( 외부 repository 사용이 안될때) + scope 정리 졸리운_곰 2020.10.05 169
59 추상(abstract) 클래스가 필요한 기본적인 이유 졸리운_곰 2019.10.30 247
58 Java and JMX - Building Manageable Systems secret 졸리운_곰 2019.05.26 0
57 [GC] 강제로 GC시키기Java 메모리 full 발생시 강제로 GC 시키기 졸리운_곰 2019.01.22 377
56 Java Map 반복(Iteration)시키는 3가지 방법 졸리운_곰 2019.01.01 347
55 UML: 클래스 다이어그램과 소스코드 매핑 file 졸리운_곰 2018.04.30 381
54 lombok에 대해서 알아보자 file 졸리운_곰 2018.04.24 292
53 lombok을 잘 써보자! (2) 졸리운_곰 2018.04.24 318
52 lombok을 잘 써보자! (1) 졸리운_곰 2018.04.24 366
51 [Java] Quartz (쿼츠)를 사용하여 자바 스케줄링(scheduling) 하기 졸리운_곰 2018.02.12 509
50 [subclipse] svn에서 무시할 파일 설정하기 svn:ignore property 설정 file 졸리운_곰 2017.09.24 286
49 자바에서 C# 호출하기 file 졸리운_곰 2017.04.26 474
48 Comparison between C# and Java - DiVA.pdf file 졸리운_곰 2017.04.25 1017
47 Java Annotation: 인터페이스 강요로부터 자유를… file 졸리운_곰 2017.03.20 302
46 Java Naming conventions 자바 명명 규칙 졸리운_곰 2017.03.04 272
45 Timer 클래스를 이용한 작업 스케쥴링 졸리운_곰 2016.11.14 290
44 자바 8 살펴보기 file 졸리운_곰 2016.05.23 432
43 Java 8 개선 사항 관련 글 모음 졸리운_곰 2016.05.23 409
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED