首页 > 解决方案 > 循环列出并更改属性 - 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;

}

标签: java

解决方案


如果您希望 totalAchievements 在应用程序对象内,则必须在应用程序实体内添加成员。

@Entity
class Application{
    @Transient
    private long totalAchievements;
}

然后设置为

userApplication.getApplication().setTotalAchievements(totalAchievements);

推荐阅读