java.lang.NullPointerException:使用QueryDsl过滤数据时

时间:2018-11-22 07:14:37

标签: spring-data-jpa querydsl

我正在使用Spring data jpa创建服务。在以下代码中,我使用Querydsl在网格上实现搜索过滤器。但是对于建筑物名称,我无法过滤网格。我得到 java.lang.NullPointerException: null

对于网格数据来自多个表。我没有为此而使用加入。我将模型类相互映射

@Service
public class RoomTransferService {

    @Autowired
    RoomTransferRepository roomTransferRepo;

    @PersistenceContext
    EntityManager em;

    public List<RoomTransfer> findRoomTransfer(String sStatus, String sBuildName, String deptFrom, String deptTo) {

        QRoomTransfer roomTransfer = QRoomTransfer.roomTransfer;

        JPAQuery<RoomTransfer> query = new JPAQuery<RoomTransfer>(em);

        query.from(roomTransfer);

        // filter the details
        if (sStatus != null) {          
            query.where(roomTransfer.sStatus.eq(sStatus));
        }

        // not filtering
        if(sBuildName !=null) {     

            query.where(roomTransfer.roomDeptMap.room.building.sBuildName.eq(sBuildName));
        }
       List<RoomTransfer> QueryResultRoomTramsfer=query.fetch();
       Return QueryResultRoomTramsfer;

客房等级

@Entity
@Table(name = "room")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Room implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "room_seq_generator")
    @SequenceGenerator(name = "room_seq_generator", sequenceName = "room_seq")
    @Column(name = "nroom_id")
    public Integer nRoomId;

    @Column(name = "ncampus_id")
    public Integer nCampusId;

    @Column(name = "nbuild_id")
    public Integer nBuildId;

    @Column(name = "ninst_id")
    public Integer nInstId;

    @Column(name = "sfloor")
    public String sFloor;

    @Column(name = "sroom_number")
    public String sRoomNumber;

    @Column(name = "sroom_desc")
    public String sRoomDesc;

    @Column(name = "scomments")
    public String sComments; 

    @Column(name = "daccepted_date")
    public Timestamp dAcceptedDate;

    @Column(name = "ssurveyor")
    public String sSurveyor;

    @Column(name = "narea")
    public Integer nArea;

    @Column(name = "ncrt_code_id")
    public Integer nCRTCodeId;

    @Column(name = "ncmn_room_bln")
    public Integer nCMNRoomBln;

    @Column(name = "nunvalidated_bln")
    public Integer nUnvalidatedBln;

    @Column(name = "sbfr_field")
    public String sBfrField;

    @Column(name = "ntemp_room_id")
    public Integer nTempRoomId;

    @Column(name = "bis_active")
    public Boolean bIsActive;

    @Column(name = "bis_jointuse")
    public Boolean bIsJointuse;

    @Column(name = "sstations_desc")
    public String sStationsDesc;

    @Column(name = "ddeleted_on")
    public Timestamp dDeletedOn;

    @Column(name = "ndeleted_by")
    public Integer nDeletedBy;

    @Column(name = "bis_incluster")
    public Boolean bIsIncluster;

    @Column(name = "bis_service_center_activity")
    public Boolean bisServiceCenterActivity;

    @Column(name = "service_center_comments")
    public String serviceCenterComments;

    @ManyToOne(optional = true)
    @JoinColumn(name = "nbuild_id", insertable = false, updatable = false)
    public Building building;

1 个答案:

答案 0 :(得分:1)

您获得的NPE可能与QueryDsl的限制有关,在该限制中您不能超过四个级别。

您的代码超过了四个级别。

roomTransfer.roomDeptMap.room.building.sBuildName

您可以在official documentation或相关stackoverflow's question上阅读更多内容。

在第一个链接中,有一个使用QueryInit注释解决此问题的解决方案。

如果您不想更改您的实体类,则可以使用前两个或第三个级别的别名执行连接(如果可能),然后使用该别名来完成查询。