2016-06-30 16 views
3

私は春のブートに基づいたアプリケーションを作成しており、そのjsonbの列にPostGreSQLのデータを格納して取得しようとしています。 それが正常に動作しますが、私はこのようなことのリポジトリインタフェース内のレコードを見つけるために基本的な方法を記述した瞬間の記録保存中: -春のブートと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] 

不思議に思うのは、postgresの通常の数値列であるidで検索しようとするとうまくいくが、json.Thisの中のキーで見つけようとするとMongoDBと正常に動作しない。

はここで書かれたBeanクラスです: -

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はkey:valueとしてJSON形式で保存された1つのドキュメントです。 AgentProfileクラスのすべてのフィールドは、それが別の/子オブジェクトであるという事実にかかわらず、Mongoにとって重要です。しかし、PostGreSQLの場合、AgentProfileオブジェクトは、このクラスを@Entityとマークしておらず、プライマリIDを持たないため、オブジェクトはString blobの1つのカラムに過ぎません。したがって、pcpAgentId=someidのようなものを検索しようとすると、PostGreSQLに意味をなさない。これは私の推測ですが、PostGreSQLのデータ構造をチェックして確認してください。

CrudRepository<AgentProfileOuter,String>は、AgentProfilOuterクラスの主キーが長いため、CrudRepository<AgentProfileOuter,long>のようになります。

+0

これは、私がspringbootを使用して、postgres.jpのjsonフィールドを照会する方法がないことを意味し、インターネット上で有用なものも見つけられませんでした。 – user3540695

+0

'AgentProfile'を' AgentProfileOuter'の子エンティティとして定義することができます。これは、各エンティティを2つの別々のテーブルに保存します。これを行うと、クエリはそのまま動作します。もう一つの選択肢として、 'like'クエリ/ spring-bootメソッドを使うことができます。これは、クエリに時間がかかります。一日の終わりに、使用したいデータベース(Mongo vs Postgre)は、どのようにデータを使用するかによって決まります。 – techtabu

+0

最近では、postgresのJSONB型についてCRUDでデータをクエリすることはまだできません。唯一の方法はネイティブクエリを実装することです。 – kensai