2016-06-30 6 views
3

스프링 부트를 기반으로하는 응용 프로그램을 작성하고 jsonb 열의 PostGreSQL db에서 데이터를 저장 및 검색하려고합니다. 기록을 저장하는 것은 잘 작동하지만 순간 내가 이런 식의 저장소 인터페이스의 기록을 찾을 수있는 기본 방법을 쓰는 동안 는 : - 다시 시작하면서Postgres와의 스프링 부트 통합 jsonb

public interface AgentProfileRepository extends CrudRepository<AgentProfileOuter,String> { 

    public AgentProfileOuter findByJdataPcpAgentId(String id); 

} 

는 서버가이 예외를주고 시작합니다 -

Caused by: java.lang.IllegalStateException: Illegal attempt to dereference path source [null.jdata] of basic type 
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final] 
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final] 
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:524) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:478) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:300) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:243) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:148) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] 

궁금해하는 점은 id에 의해 정상적인 숫자 열이 postgres에서 찾으려고 할 때 그것은 잘 작동하지만 json.This 안에있는 키로 찾으려고하면 MongoDB와 성공적으로 작동합니다.

여기에 기록 된 콩 클래스입니다 : -

AgentProfileOuter.java

import javax.persistence.Column; 
import javax.persistence.Convert; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 



@Table(name = "Agentbonds") 
@Entity 

public class AgentProfileOuter { 


    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Id 
    private long id; 

    @Convert(converter = ConverterAgent.class) 
    @Column(name="jdata") 
    private AgentProfile jdata; 





    public long getId() { 
     return id; 
    } 

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

    public AgentProfile getJdata() { 
     return jdata; 
    } 

    public void setJdata(AgentProfile jdata) { 
     this.jdata = jdata; 
    } 

} 

AgentProfile.java

import java.util.HashMap; 
import java.util.Map; 

import javax.annotation.Generated; 
import javax.persistence.Convert; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Table; 

import org.springframework.data.annotation.Id; 

import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@Generated("org.jsonschema2pojo") 
@JsonPropertyOrder({ 
    "pcpAgentId", 
    "name", 
    "bio", 
    "phone", 
    "email", 
    "sms", 
    "imageUrl" 
}) 





public class AgentProfile { 



    @JsonProperty("pcpAgentId") 
    private String pcpAgentId; 

    /* 
    public void setAdditionalProperties(Map<String, Object> additionalProperties) { 
     this.additionalProperties = additionalProperties; 
    } 
    */ 

    @JsonProperty("name") 
    private String name; 
    @JsonProperty("bio") 
    private String bio; 
    @JsonProperty("phone") 
    private String phone; 
    @JsonProperty("email") 
    private String email; 
    @JsonProperty("sms") 
    private String sms; 
    @JsonProperty("imageUrl") 
    private String imageUrl; 
    @JsonIgnore 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    /** 
    * 
    * @return 
    *  The pcpAgentId 
    */ 
    @JsonProperty("pcpAgentId") 
    public String getpcpAgentId() { 
     return pcpAgentId; 
    } 

    /** 
    * 
    * @param pcpAgentId 
    *  The pcpAgentId 
    */ 
    @JsonProperty("pcpAgentId") 
    public void setAgentId(String pcpAgentId) { 
     this.pcpAgentId = pcpAgentId; 
    } 

    /** 
    * 
    * @return 
    *  The name 
    */ 
    @JsonProperty("name") 
    public String getName() { 
     return name; 
    } 

    /** 
    * 
    * @param name 
    *  The name 
    */ 
    @JsonProperty("name") 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * 
    * @return 
    *  The bio 
    */ 
    @JsonProperty("bio") 
    public String getBio() { 
     return bio; 
    } 

    /** 
    * 
    * @param bio 
    *  The bio 
    */ 
    @JsonProperty("bio") 
    public void setBio(String bio) { 
     this.bio = bio; 
    } 

    /** 
    * 
    * @return 
    *  The phone 
    */ 
    @JsonProperty("phone") 
    public String getPhone() { 
     return phone; 
    } 

    /** 
    * 
    * @param phone 
    *  The phone 
    */ 
    @JsonProperty("phone") 
    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    /** 
    * 
    * @return 
    *  The email 
    */ 
    @JsonProperty("email") 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * 
    * @param email 
    *  The email 
    */ 
    @JsonProperty("email") 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    /** 
    * 
    * @return 
    *  The sms 
    */ 
    @JsonProperty("sms") 
    public String getSms() { 
     return sms; 
    } 

    /** 
    * 
    * @param sms 
    *  The sms 
    */ 
    @JsonProperty("sms") 
    public void setSms(String sms) { 
     this.sms = sms; 
    } 

    /** 
    * 
    * @return 
    *  The imageUrl 
    */ 
    @JsonProperty("imageUrl") 
    public String getImageUrl() { 
     return imageUrl; 
    } 

    /** 
    * 
    * @param imageUrl 
    *  The imageUrl 
    */ 
    @JsonProperty("imageUrl") 
    public void setImageUrl(String imageUrl) { 
     this.imageUrl = imageUrl; 
    } 

    @JsonAnyGetter 
    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    @JsonAnySetter 
    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 

이에 어떤 도움이 크게 감사합니다.

답변

1

Mongo와 PostGreSQL이 어떻게 데이터를 구성했는지 생각합니다. Mongo의 점에서 AgentProfileOuter은 JSON 형식으로 key : value로 저장된 하나의 문서입니다. AgentProfile 클래스의 모든 필드는 다른/자식 객체라는 사실과 상관없이 Mongo의 핵심입니다. 그러나 PostGreSQL의 경우 전체 AgentProfile 객체는 @Entity으로 표시되지 않고 기본 ID가 없으므로 객체는 String BLOB의 단 하나의 열입니다. 따라서 pcpAgentId=someid과 같은 것을 검색하려고하면 PostGreSQL에 아무런 의미가 없습니다. 이것은 내 생각에 PostGreSQL의 데이터 구조를 확인하여 확인하십시오.

또한 CrudRepository<AgentProfileOuter,String>AgentProfilOuter 클래스의 기본 키가 길기 때문에 CrudRepository<AgentProfileOuter,long>과 같아야합니다.

+0

즉, postgres.js에서 json 필드를 쿼리하기 위해 springboot를 사용할 수있는 방법이 없다는 것을 의미합니다. 인터넷에서 유용한 정보도 찾을 수 없습니다. – user3540695

+0

'AgentProfile'을'AgentProfileOuter'의 하위 엔티티로 정의 할 수 있습니다.이 엔티티는 각 엔티티를 별도의 두 테이블에 저장합니다. 그렇게하면 쿼리가 그대로 작동합니다. 또 다른 옵션으로'like' query/spring-boot 메쏘드를 사용할 수 있습니다. 이것은 우스꽝스러운 시간을 들여 쿼리합니다. 하루가 끝날 때 사용하려는 데이터베이스 (Mongo vs Postgre)는 데이터 사용 방법에 따라 다릅니다. – techtabu

+0

요즘에는 CRUD 방식으로 데이터를 쿼리 할 수 ​​없지만 Postgres의 JSONB 유형은 원시 쿼리를 구현하는 유일한 방법입니다. – kensai