在java spring数据中获取具有零int值的瞬态字段

时间:2016-09-27 15:02:45

标签: java spring rest spring-data-jpa

我们通过控制器获取请求。该模型包含在控制器中设置的瞬态字段。控制器仅返回非空或> 0的int字段。如何允许瞬态字段返回0值,因为这是有意义的。在这种情况下,瞬态字段是" sentenceStart'和' sentenceEnd'。

控制器:

@RequestMapping(value = "/{match_id}", method = RequestMethod.GET)
public Match getMatch(@PathVariable("match_id") long matchId) {
    long start = System.currentTimeMillis();
    LOGGER.info("REQUESTED RETRIEVAL OF MATCH WITH ID: " + matchId);
    Match match = matchRepository.findOneById(matchId);
                match.setActualText(matchRepository.getText(match.getId()));
  match.setSentenceStart(matchRepository.getSentenceStart(match.getId()));    
    match.setSentenceEnd(matchRepository.getSentenceEnd(match.getId()));
    match.setSentenceID(matchRepository.getSentenceId(match.getId()));
    long end = System.currentTimeMillis();
    LOGGER.info("DONE. TOOK " + (end - start) + " MILLISECONDS.");
    return match;
} //getMatch()

存储库:

public interface MatchRepository extends JpaRepository<Match, Long>, JpaSpecificationExecutor<Match> {

@Query(value = "SELECT match_in_sentence_start FROM vTextMatch where match_id = :m_id LIMIT 1",
        nativeQuery = true)
int getSentenceStart(@Param("m_id") long matchId);

@Query(value = "SELECT match_in_sentence_end  FROM vTextMatch where match_id = :m_id LIMIT 1",
        nativeQuery = true)
int getSentenceEnd(@Param("m_id") long matchId);

}

型号:

@Entity
@Table(name = "match_term")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Match {

@Id
@GeneratedValue
@Column(name = "match_id", nullable = false)
private Long id;
@Column(name = "document_id", nullable = false)
private Long documentId;
@Column(name = "document_start")
private Integer documentStart;
@Column(name = "document_end")
private Integer documentEnd;
@Column(name = "is_meta", nullable = false)
private Boolean isMeta;
@Column(name = "date_inserted", nullable = false)
private Timestamp dateInserted;

@Transient
private String actualText;
@Transient
private int sentenceStart;
@Transient
private int sentenceEnd;
@Transient
private int sentenceID;

/*
|-------------------|
| AUXILIARY METHODS |
|-------------------|
 */

/*
|-------------------|
|SETTERS ANG GETTERS|
|-------------------|
 */

public int getSentenceStart() {
    return sentenceStart;
}

public void setSentenceStart(int sentenceStart) {
    this.sentenceStart = sentenceStart;
}

public int getSentenceEnd() {
    return sentenceEnd;
}

public void setSentenceEnd(int sentenceEnd) {
    this.sentenceEnd = sentenceEnd;
}
}

1 个答案:

答案 0 :(得分:1)

你应该调查包含@JsonInclude的行(JsonInclude.Include.NON_EMPTY) - http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html#NON_EMPTY

我建议稍微更改您的设计并介绍一些消息类型。您可以避免使用不需要的东西来污染您的域类。