2014-08-29 3 views
0

가 나는 몽고의 컬렉션을 나타냅니다 클래스가 "아카이브"라고했다스프링 데이터 - MongoRepository : _id로 바이너리 타입을 사용할 수 있습니까?

@Document(collection = "archive") 

public class Message{ 
    @Id 
    private byte[] messageId; 

    private String from; 
    private String to; 
    // more stuff 

} 

인터페이스 MessagesRepository가 MongoRepository 확장 : API 호출을 통해

public interface MessagesRepository extends MongoRepository<Message, String>{ 

} 

을, 나는 나을 제공하는 findMessage 요청을 얻을 messageId in String. 그런 다음 byte []로 인코딩 한 다음 messagesRepository.findOne() 메서드를 호출해야합니다. ID는 byte[]입니다.

실패합니다. null을 반환합니다. 나는 byte []이 Mongo에 저장되어 있기 때문에 findOne() 메서드 내에서 byte[]과 다를 것입니다. 동일한 값을 가진 다른 문자열도 다른 byte[] 배열을 생성하기 때문입니다.

어떻게하면됩니까? 또는 바이너리로 _id로 작업하는 것이 실제로 가능합니까?

+0

ID 필드의 논리적 데이터 유형은 무엇입니까? – chrylis

+0

@chrylis : OP는 바이트 []임을 말했다; @ "Sasanka Panguluri": _id에 바이너리/바이트를 사용하여 호의적으로 행동하지 않습니다. 왜 대신 메시지 문자열 또는 그 hashCode를 사용하지 않을까요? – Seismoid

+0

@Seismoid 내가 논리적 인 ** 유형에 대해 질문 한 이유가 있습니다. 즉시 선언 된 타입은'byte []'입니다. 그리고'hashCode'를 사용하는 것은 어리석은 짓입니다. 데이터베이스 ID는 고유해야합니다. – chrylis

답변

0

음, 여전히 효과가 있습니다. 나는 byte[]_id으로 가질 수있었습니다. 성공적으로 물건을 넣고 꺼낼 수있었습니다. find()의 문제점은 MongoRepository에 있습니다. 그것은 작동하도록 조정해야합니다.

은 참조 : http://pastebin.com/xHVQJYfN

편집 : 나는 최신 드라이버를 가지고 그것은 바로했다.

0

당신은 byte[]을 ID로 사용하여 자신을 호의적으로하지 않습니다.

당신은 메시지가 String으로 표시됩니다. 사용하지 않으시겠습니까? 그렇지 않으면 문자열과 바이트 유형간에 캐스팅이 끝날 수 있습니다 (byte[]을 사용하는 것이 자신이 선택한 것이 아니라 사용자에게 주어진 제약이 아니라면). 동일한 문자열에서 생성 된 바이트가 일치하지 않는다는 것을 알았습니다.

짧은 예 :

public static void main(String[] args) throws Exception { 
    String s1 = "hello"; 
    String s2 = "hello"; 

    System.out.println("our two strings: "); 
    System.out.println(s1); 
    System.out.println(s2); 

    // one of the first thing we learned when starting with 
    // java was that this is not equal 
    System.out.println(); 
    System.out.println("compare with =="); 
    if(s1==s2) System.out.println("equal"); 
    else System.out.println("not equal"); 

    // but this is equal 
    System.out.println("compare with equals()"); 
    if(s1.equals(s2)) System.out.println("equal"); 
    else System.out.println("not equal"); 

    // create the byte arrays and compare them 
    byte[] b1 = s1.getBytes(); 
    byte[] b2 = s2.getBytes(); 

    System.out.println(); 
    System.out.println("byte array 1: " + b1.toString()); 
    System.out.println("byte array 2: " + b2.toString()); 

    // same as for strings 
    System.out.println("compare with =="); 
    if(b1==b2) System.out.println("equal"); 
    else System.out.println("not equal"); 

    // not equal, unlike the strings from which we 
    // created the byte arrays 
    System.out.println("compare with equals()"); 
    if(b1.equals(b2)) System.out.println("equal"); 
    else System.out.println("not equal"); 


    // create string out of the bytes again and compare 
    String ss1 = new String(b1, "UTF-8"); 
    String ss2 = new String(b2, "UTF-8"); 

    System.out.println(); 
    System.out.println("re-created string 1: " + ss1); 
    System.out.println("re-created string 2: " + ss2); 

    // this is equal again 
    System.out.println("compare re-created strings with equals()"); 
    if(ss1.equals(ss2)) System.out.println("equal"); 
    else System.out.println("not equal"); 
} 

내가 그것을 바이트하는 이유를 묻는 이유; 모든 것을 힘들게 만들고 있습니다.

+0

맞아, 고마워. 그러나 몽고의 공간을 절약하기 위해 내가 할 수있는 일이 무엇입니까? –

관련 문제