2016-05-31 79 views
0

我使用elasticsearch,SecurityApp實體:Elasticsearch不按照createdBy字段查詢?

public class SecurityApp extends AbstractAuditingEntity implements Serializable { 

private static final long serialVersionUID = 1L; 

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

@NotNull 
@Size(max = 50) 
@Column(name = "app_name", length = 50, nullable = false) 
private String appName; 

@Size(max = 20) 
@Column(name = "app_key", length = 20, updatable = false) 
private String appKey; 

@Size(max = 20) 
@Column(name = "app_secret", length = 20, updatable = false) 
private String appSecret; 
// hide getter and setter 
} 

AbstractAuditingEntity實體:

public abstract class AbstractAuditingEntity implements Serializable { 

private static final long serialVersionUID = 1L; 

@CreatedBy 
@Column(name = "created_by", nullable = false, length = 50, updatable = false) 
// @JsonIgnore 
private String createdBy; 

@CreatedDate 
@Column(name = "created_date", nullable = false) 
@JsonIgnore 
private ZonedDateTime createdDate = ZonedDateTime.now(); 

@LastModifiedBy 
@Column(name = "last_modified_by", length = 50) 
@JsonIgnore 
private String lastModifiedBy; 

@LastModifiedDate 
@Column(name = "last_modified_date") 
@JsonIgnore 
private ZonedDateTime lastModifiedDate = ZonedDateTime.now(); 
// hide getter and setter 
} 

ElasticSearchConfiguration:

@Configuration 
@AutoConfigureAfter(value = { JacksonConfiguration.class }) 
public class ElasticSearchConfiguration { 

@Bean 
public ElasticsearchTemplate elasticsearchTemplate(Client client, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) { 
    return new ElasticsearchTemplate(client, new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build())); 
} 

public class CustomEntityMapper implements EntityMapper { 

    private ObjectMapper objectMapper; 

    public CustomEntityMapper(ObjectMapper objectMapper) { 
     this.objectMapper = objectMapper; 
     objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
     objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 
    } 

    @Override 
    public String mapToString(Object object) throws IOException { 
     return objectMapper.writeValueAsString(object); 
    } 

    @Override 
    public <T> T mapToObject(String source, Class<T> clazz) throws IOException { 
     return objectMapper.readValue(source, clazz); 
    } 
} 
} 

我的問題: 當我使用的查詢字段APPNAME,它作品。而當我使用查詢字段createdBy,它不工作! 這是爲什麼?

回答

1

我找到了解決方案。

因爲@JsonIgnore註解和私人權限。

當我更新這樣的代碼,並且resave數據,它的工作原理。

@CreatedBy 
@Column(name = "created_by", nullable = false, length = 50, updatable = false) 
// @JsonIgnore 
protected String createdBy; 

也許elasticsearch使用JSON?採取反思不可用延伸私人領域?我不知道。

好消息。有用。

+0

是的,在JHipster應用程序中,Elasticsearch將對象存儲爲JSON並使用Jackson進行序列化/反序列化,因此任何帶有@ JsonIgnore的字段都不會被索引,也不會被搜索到。 – geraldhumphries

+0

@geraldhumphries爲什麼字段的權力是私人的,它不起作用 –

+0

根據我的經驗,它只是通過刪除JsonIgnore。你有公共的getter和setter嗎? – geraldhumphries