java - 循环列出并更改属性 - Java
问题描述
我正在尝试计算运行时的成就数量并将其设置到页面内的应用程序对象中,但 totalAchivements 超出了对象。
基本上totalAchievements
应该在每个应用程序内部。
控制器
Page<UserApplication> userApplications = userApplicationRepository.findByUserUsername(pageable, username);
for(UserApplication userApplication : userApplications.getContent()) {
long totalAchievements = achievementRepository.countByApplicationApplicationId(userApplication.getApplication().getApplicationId());
userApplication.setTotalAchievements(totalAchievements);
}
return new ResponseEntity<>(userApplications, HttpStatus.OK);
模型
@Entity
@Table(name = "USER_APPLICATION")
public class UserApplication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "userId", referencedColumnName="userId")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIgnore
private User user;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "applicationId", referencedColumnName="applicationId")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Application application;
@Transient
private long totalAchievements;
}
解决方案
如果您希望 totalAchievements 在应用程序对象内,则必须在应用程序实体内添加成员。
@Entity
class Application{
@Transient
private long totalAchievements;
}
然后设置为
userApplication.getApplication().setTotalAchievements(totalAchievements);
推荐阅读
- android - 锁定和解锁手机后 ViewModel 被销毁
- python - 无法在 Mac 上使用 pip3 install pygame 安装 pygame
- linux - 在 Gentoo 上安装 ElasticSearch
- java - 传递 SelectOneMenu 值以在 JSF/Primefaces 中添加功能
- java - 由应用程序 [webservices] 中的 servlet [REST 服务] 创建的未捕获的 init() 异常:java.lang.IllegalArgumentException
- r - 从R中的嵌套ggplot中删除索引号
- android - 将垂直线添加到 ProgressBar
- postgresql - 在 AWS 上创建具有只读权限的 PostgreSQL 用户
- c - 为什么通过引用传递在 c 中不起作用?
- ios - 子类 UINavigationBar SWIFT