spring - 为 CrudRepository (findById) 的一种方法返回两种不同的数据类型
问题描述
我正在使用 CrudRepository。
我在两种情况下需要方法 findById:
- EmailShort findById(Long id);
- EmailFull findById(Long id);
EmailShort 和 EmailFull 是接口。
public interface EmailFull extends EmailCustom {
Long getId();
UserShort getSender();
String getContent();
String getTopic();
String getShortContent();
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
Date getCreationTime();
}
public interface EmailShort extends EmailCustom {
Long getId();
UserShort getSender();
String getTopic();
String getShortContent();
boolean getIsRead();
void setRead(boolean read);
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
Date getCreationTime();
}
我的仓库:
@Repository
public interface EmailDao extends CrudRepository<Email, Long> {
EmailShort findById(Long id);
@Query("select e from email e where e.id=?1")
EmailFull findFullById(@Param("id") Long id);
@Query(value = "select * " +
"from email " +
"where email.id = :emailId", nativeQuery = true)
EmailShort findByIdWithShortSenderInfo(@Param("emailId") long emailId);
}
我不能使用电子邮件对象,因为它包含带有地址等数据的用户(不需要它)。
任何想法如何解决这个问题?Maybie 我应该更改实体定义吗?
解决方案
推荐阅读
- flutter - flutter popUntil 从最后三个路由调用 initState
- mobile - jquery-one-page-nav 在移动设备上不起作用
- python - 如何使用 Selenium 在文本字段中插入值?
- wxpython - sizer 中的 WxWidgets 面板似乎无法正常工作
- performance - OpenMP 加速损失
- javascript - 每次路由页面时如何使用 res.render 发送数据?
- python - 根据 n 个连续条目替换列中的值
- r - 包装 arma::Mat 时如何避免深度复制
到 Rcpp::NumericMatrix - ansible - 如何使用 ansible playbook 运行合规性检查
- r - 在R中使用字符串名称访问命名嵌套列表元素