0

I am using Spring data jpa for creating service. In following code I am using Querydsl for implementing search filter on grid. But for building name I am not able to filter the grid. I am getting java.lang.NullPointerException: null.

For grid data is coming from multiple tables. I am not using joining for that. I mapped models classes with each other

@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;

Room class

@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;
SpringUser
  • 1,351
  • 4
  • 29
  • 59
  • Did you debug your code? What is null? roomTransfer.roomDeptMap.room.building.sBuildName here are lots of possible null values... – C. Weber Nov 22 '18 at 07:18

1 Answers1

2

The NPE that you get is probably related to the limitation of QueryDsl where you cannot exceed four levels.

Your code exceeds four levels.

roomTransfer.roomDeptMap.room.building.sBuildName

You can read more on official documentation or related stackoverflow's question.

In the first link there is a solution to overcome this issue using QueryInit annotation.

If you do not want to alter your entity class you can perform a join (if possible) with an alias for the first 2 or 3 levels and then use this alias to complete the query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
leopal
  • 4,711
  • 1
  • 25
  • 35
  • I added Room class Can you tell me how I can use `QueryInit` – SpringUser Nov 22 '18 at 08:13
  • Can I use like This `@QueryInit("building.sBuildName") @ManyToOne(optional = true) @JoinColumn(name = "nbuild_id", insertable = false, updatable = false) public Building building;` – SpringUser Nov 22 '18 at 08:20
  • I haven't used this annotation before but you can try what you are asking above. Because I do not want to change my entity class I always perform a join. So try this: `query.join(roomTransfer.roomDeptMap,someAlias).where(someAlias.room.building...)`. I am not aware of your `RoomTransfer` class so you have to edit join according to your class. – leopal Nov 22 '18 at 08:28