gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:27:30 24 4
gpt4 key购买 nike

我们正在通过 Controller 发出获取请求。该模型包含在 Controller 中设置的 transient 字段。 Controller 仅返回不为 null 或 >0 的 int 字段。我如何允许 transient 字段返回 0 值,因为这很有意义。在本例中, transient 字段为“sentenceStart”和“sentenceEnd”。

Controller :

@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;
}
}

最佳答案

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

我建议稍微改变你的设计并引入一些消息类型。您可以避免不需要的东西污染您的域类。

关于java - 在java spring数据中获取具有零int值的 transient 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com