You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate异常行为:集合返回null而非空列表问题排查

问题描述

数据库结构示意:MonitorUser关联多个MonitoredUser;MonitoredUser关联Take集合(takeList)和IsMonitoredBy集合(isMonitoredByList),同时IsMonitoredBy关联MonitoredUser和Device。

执行findMonitorUserById(userId)方法时,Hibernate应返回指定ID的MonitorUser及其关联的MonitoredUsers列表。按Hibernate默认逻辑,集合属性即便没有数据也应是空列表而非null,但调试发现MonitoredUser的takeList符合预期(空列表),但isMonitoredByList却为null。请问这是代码错误还是配置问题?

调试时观察到的monitorUser对象状态:monitoredUserList中的每个MonitoredUser实例,takeList为[],isMonitoredByList为null。


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);
    }

}

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
}

内容的提问来源于stack exchange,提问作者Luca Lotifi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:45:09