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

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
284 Demonstrates CellEditors : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 38
283 Demonstrates TableViewers : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 60
282 Java and JMX - Building Manageable Systems secret 졸리운_곰 2019.05.26 0
281 Single_Sourcing_RAP_RCP_en.pdf file 졸리운_곰 2019.05.15 27
280 Rich client platform 설명 및 배우기 참고 졸리운_곰 2019.05.15 89
279 Rich Ajax Platform, Part 1: 소개 file 졸리운_곰 2019.05.15 127
278 또 하나의 크로스 플랫폼: Eclipse RAP file 졸리운_곰 2019.05.15 143
277 Eclipse 4 RCP 튜토리얼(완료) file 졸리운_곰 2019.05.14 682
276 [JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..) , 페이징처리 졸리운_곰 2019.03.24 1614
275 스프링 데이터 JPA 레퍼런스 번역 file 졸리운_곰 2019.03.24 1013
274 JPA 개념, class05 JPA 환경설정 졸리운_곰 2019.03.24 53
273 [자바코드] 고유값인 UUID, GUID 생성하기 졸리운_곰 2019.02.27 248
272 [JPA] 복합키 졸리운_곰 2019.02.26 40
271 Spring Batch Multithreading Example file 졸리운_곰 2019.01.31 77
270 Spring batch를 Parallel로 돌려보자 졸리운_곰 2019.01.31 88
269 [GC] 강제로 GC시키기Java 메모리 full 발생시 강제로 GC 시키기 졸리운_곰 2019.01.22 224
268 Java Map 반복(Iteration)시키는 3가지 방법 졸리운_곰 2019.01.01 65
» jpa muli row select result is same row repeat Java 자바 Jpa에서 멀티 로우 반환시 같은값이 반복 file 졸리운_곰 2019.01.01 153
266 [자바] 리스트를 순회하는 방법 졸리운_곰 2018.12.31 67
265 SpringBoot JPA 예제 졸리운_곰 2018.12.31 55
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED