Spring Boot 奇怪的休眠行为:空列表而不是空列表

huwehgph  于 2023-03-18  发布在  Spring
关注(0)|答案(1)|浏览(171)

Db架构如下所示:

当我运行findMonitorUserById(userId)函数时,hib应该返回一个列表,其中包含与MonitorUser关联的MonitoredUsersid=userIdtakeListisMonitoredByList。默认情况下,我应该总是有一个最多为空的集合,但永远不会为空,对吗?通过调试,我发现,确实takeList遵守了此语句,但isMonitoredByList不遵守。这是由于我的错误吗?配置问题?
实现方法如下:
Controller.java

@RestController
public class Controller {

@Autowired
IUserService userService;

    @PostMapping(value = "/addMonitoredUser", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackOn = SaveException.class)
    public ResponseEntity<MonitorUserDTO> addMonitoredUser(@RequestBody MonitoredUserDTO monitoredUserDTO) throws DrugNotFoundException, SaveException, UserNotFoundException {

        MonitorUser monitorUser = userService.findMonitorUserById(monitoredUserDTO.getMonitorUser().getId());
        MonitorUserDTO monitorUserDTO = setMonitorDTO(monitorUser);

        return ResponseEntity
                .ok()
                .body(monitorUserDTO);
    }

}

如果我检查monitorUser,我可以看到:

UserService.java

@Service
public class UserService implements IUserService {
    @Autowired
    UserRepository userRepository;

    @Override
    @Transactional
    public MonitorUser findMonitorUserById(int idUser) throws UserNotFoundException {
        MonitorUser res = userRepository.findMonitorUserById(idUser);

        if(res == null)
            throw new UserNotFoundException("UserService findMonitorUserById(" +idUser +"): not found");
        return res;
    }

UserRepository.java

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

    MonitorUser findMonitorUserById(int idMonitor);

}

最后,下面是实体类:
MonitorUser.java.

@Entity
@DiscriminatorValue("monitor")
public class MonitorUser extends User{
    public MonitorUser() {
    }

    String username;
    String password;

    @OneToMany(mappedBy = "monitorUser", targetEntity = Monitor.class, cascade = CascadeType.ALL , fetch = FetchType.LAZY )
    List<Monitor> monitorList;

    @OneToMany(mappedBy = "monitorUser", targetEntity = MonitoredUser.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    List<MonitoredUser> monitoredUserList;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Monitor> getMonitorList() {
        return monitorList;
    }

    public void setMonitorList(List<Monitor> monitorList) {
        this.monitorList = monitorList;
    }

    public List<MonitoredUser> getMonitoredUserList() {
        return monitoredUserList;
    }

    public void setMonitoredUserList(List<MonitoredUser> monitoredUserList) {
        this.monitoredUserList = monitoredUserList;
    }
}

MonitoredUser.java

@Entity
@DiscriminatorValue("monitored")
public class MonitoredUser extends User{
    public MonitoredUser() {
    }

    float weight;
    int height; //in cm
    @Enumerated(EnumType.STRING)
    Gender sex;
    boolean smoker;
    String phoneNumber;
    int nyhaclass;
    @OneToMany(mappedBy = "monitoredUser", targetEntity = Take.class, cascade = CascadeType.ALL , fetch = FetchType.LAZY )
    List<Take> takeList;
    @OneToMany(mappedBy = "monitoredUser", targetEntity = IsMonitoredBy.class, cascade = CascadeType.ALL , fetch = FetchType.LAZY )
    List<IsMonitoredBy> isMonitoredByList;
    boolean hospitalized;
    @ManyToOne
    MonitorUser monitorUser;

    //and all the getter and setter

}

IsMonitoredBy.java

@Entity

public class IsMonitoredBy {

    public IsMonitoredBy() {
    }

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    int id;
    long lastSyncTimeStamp;

    @ManyToOne
    MonitoredUser monitoredUser;
    @ManyToOne
    Device device;

    long timestampStart;
    long timestampEnd;

//all the getter an setter
}
6tr1vspr

6tr1vspr1#

换做这个吧!!

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

    List<MonitorUser> findMonitorUserById(int idMonitor);

}

相关问题