我正在尝试计算运行时的成就量并将其设置到页面内的应用程序对象中,但是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位于应用程序对象内,则必须在Application实体内添加成员。
@Entity
class Application{
@Transient
private long totalAchievements;
}
然后将其设置为
userApplication.getApplication().setTotalAchievements(totalAchievements);