Spring JPA - 方法 "getById" 无效,我得到 null
Spring JPA - method "getById" is not working, I get null
我正在尝试通过 ID 获取对象,但我无法弄清楚为什么我得到 null 并且它不起作用..
@Entity
@Table(name = "exercises")
public class ExerciseEntity {
@Id
private Long id;
private String nameOfExercise;
private Integer caloriesBurnedForHour;
@Column(columnDefinition = "TEXT")
private String bonusInfo;
@ManyToMany(mappedBy = "exerciseEntityList",fetch = FetchType.EAGER)
private List<PersonalTrainingProgram> personalTrainingPrograms;
public ExerciseEntity() {
}
@Entity
public class PersonalTrainingProgram {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany
@JoinTable()
private List<ExerciseEntity> exerciseEntityList;
@ManyToOne
private FoodProgram foodProgram;
@ManyToOne
private AppUser appUser;
public PersonalTrainingProgram() {
}
这是方法,
@Override
public List<ExerciseEntity> findExercisesBySpecificId(List<ExerciseDto> exerciseDto) {
List<ExerciseEntity> listEntity= new ArrayList<>();
for (int i = 0; i <exerciseDto.size() ; i++) {
ExerciseEntity byId = exercisesRepository.getById(exerciseDto.get(i).getId());
listEntity.add(byId);
}
return listEntity;
}
@Override
public void addItToDatabase(List<ExerciseDto> exerciseDto, FoodProgramDto
foodProgramDto,String username) {
PersonalTrainingProgram personalTrainingProgram = new PersonalTrainingProgram();
personalTrainingProgram.setExerciseEntityList(findExercisesBySpecificId(exerciseDto));
personalTrainingProgram.setFoodProgram(foodProgramService
.findFoodProgramById(foodProgramDto));
personalTrainingProgram.setAppUser(appUserRepository.findByUsername(username).get());
personalTrainingRepository.save(personalTrainingProgram);
}
在调试模式下练习行后我得到这个“{ExerciseEntity$HibernateProxy$B...}”
我认为这可能来自关系,但对我来说一切似乎都很好。任何帮助将不胜感激
存储库是标准存储库,扩展了 JPA
您必须使用 findById
,returns 是可选的。
getById returns 不是实体而是引用。
来自文档:
Returns a reference to the entity with the given identifier. Depending
on how the JPA persistence provider is implemented this is very likely
to always return an instance and throw an EntityNotFoundException on
first access. Some of them will reject invalid identifiers
immediately.
所以一直是proxy可能没有初始化导致问题
我正在尝试通过 ID 获取对象,但我无法弄清楚为什么我得到 null 并且它不起作用..
@Entity
@Table(name = "exercises")
public class ExerciseEntity {
@Id
private Long id;
private String nameOfExercise;
private Integer caloriesBurnedForHour;
@Column(columnDefinition = "TEXT")
private String bonusInfo;
@ManyToMany(mappedBy = "exerciseEntityList",fetch = FetchType.EAGER)
private List<PersonalTrainingProgram> personalTrainingPrograms;
public ExerciseEntity() {
}
@Entity
public class PersonalTrainingProgram {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany
@JoinTable()
private List<ExerciseEntity> exerciseEntityList;
@ManyToOne
private FoodProgram foodProgram;
@ManyToOne
private AppUser appUser;
public PersonalTrainingProgram() {
}
这是方法,
@Override
public List<ExerciseEntity> findExercisesBySpecificId(List<ExerciseDto> exerciseDto) {
List<ExerciseEntity> listEntity= new ArrayList<>();
for (int i = 0; i <exerciseDto.size() ; i++) {
ExerciseEntity byId = exercisesRepository.getById(exerciseDto.get(i).getId());
listEntity.add(byId);
}
return listEntity;
}
@Override
public void addItToDatabase(List<ExerciseDto> exerciseDto, FoodProgramDto
foodProgramDto,String username) {
PersonalTrainingProgram personalTrainingProgram = new PersonalTrainingProgram();
personalTrainingProgram.setExerciseEntityList(findExercisesBySpecificId(exerciseDto));
personalTrainingProgram.setFoodProgram(foodProgramService
.findFoodProgramById(foodProgramDto));
personalTrainingProgram.setAppUser(appUserRepository.findByUsername(username).get());
personalTrainingRepository.save(personalTrainingProgram);
}
在调试模式下练习行后我得到这个“{ExerciseEntity$HibernateProxy$B...}” 我认为这可能来自关系,但对我来说一切似乎都很好。任何帮助将不胜感激
存储库是标准存储库,扩展了 JPA
您必须使用 findById
,returns 是可选的。
getById returns 不是实体而是引用。
来自文档:
Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw an EntityNotFoundException on first access. Some of them will reject invalid identifiers immediately.
所以一直是proxy可能没有初始化导致问题