Reputation: 1439
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;
Upvotes: 0
Views: 1300
Reputation: 4959
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.
Upvotes: 2