我有@Entity
@Table(name = "orgtree")
public class OrganizationTree {
@Id
@Column(name="ORGANIZATION_ID")
private String organizationId;
@Column(name="ORGANIZATION_NAME")
private String organizationName;
}
@RepositoryRestResource(collectionResourceRel = "organizationTree", path = "organizationTree")
public interface OrganizationTreeRepository extends JpaRepository<OrganizationTree,String> {
@Query
@RestResource(path = "findAll", rel = "findAll")
List<OrganizationTree> findAll();
}
以及提供REST访问的存储库
@Autowired
@Transient
private OrgTreeService orgTreeService;
@JsonSerialize
public Integer getPersonCount() {
return orgTreeService.getPersonCount(organizationId);
}
到目前为止一切顺利。
现在我想将一个计算字段添加到我的实体
SELECT * FROM msg_messages m
JOIN msg_status s on m.messageId = s.messageId
JOIN msg_threads t ON t.threadId = m.threadId
JOIN users u ON u.userId = s.userId
WHERE t.threadId = 1
GROUP BY u.userId ORDER BY `m`.`date` DESC
这里有几个问题:
这个问题的规范解决方案是什么?
答案 0 :(得分:3)
我找到的解决方案(或者我应该称之为黑客攻击)如下:
我使用自定义序列化程序注释计算字段:
@Formula(value = "ORGANIZATION_ID")
@JsonSerialize(using=JsonOrgPersonCountSerializer.class)
private String personCount;
在序列化程序中,我计算了人数:
public class JsonOrgPersonCountSerializer extends JsonSerializer<String> {
@Override
public void serialize(String source, JsonGenerator gen, SerializerProvider prov) throws IOException, JsonProcessingException {
gen.writeString("" + orgTreeService.getPersonCount(source));
}
}
另一种解决方案是使用某种数据传输对象,我可以在其中调用我的服务。