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;
1
  • Did you debug your code? What is null? roomTransfer.roomDeptMap.room.building.sBuildName here are lots of possible null values... Commented Nov 22, 2018 at 7:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I added Room class Can you tell me how I can use QueryInit
Can I use like This @QueryInit("building.sBuildName") @ManyToOne(optional = true) @JoinColumn(name = "nbuild_id", insertable = false, updatable = false) public Building building;
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.