When I am trying to update a field of table using @Query(value = "Update ",nativeQuery=true) is throwing null pointer exception.
I have tried many approaches which I can find in stack overflow, but no one solves my problem.
AirCraftRoute.java
@Builder
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "aircraft_route")
public class AirCraftRoute implements Serializable
{
@EmbeddedId
private AirCraftRoutePK pk = new AirCraftRoutePK();
@ManyToOne
@MapsId("airCraftId")
@JoinColumn(name = "aircraft_id")
private AirCraft airCraft;
@ManyToOne
@MapsId("routeId")
@JoinColumn(name = "route_id")
private Route route;
@Column(name = "journey_date")
private Date journeyDate;
@Column(name = "departure_time")
private Time departureTime;
@Column(name = "arrival_time")
private Time arrivalTime;
@Column(name = "fare")
private float fare;
@Column(name = "availability")
private int availability;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AirCraftRoute)) return false;
AirCraftRoute that = (AirCraftRoute) o;
return Float.compare(that.getFare(), getFare()) == 0 &&
Objects.equals(getAirCraft(), that.getAirCraft()) &&
Objects.equals(getRoute(), that.getRoute()) &&
Objects.equals(getJourneyDate(), that.getJourneyDate()) &&
Objects.equals(getDepartureTime(), that.getDepartureTime()) &&
Objects.equals(getArrivalTime(), that.getArrivalTime());
}
@Override
public int hashCode() {
return Objects.hash(getAirCraft(), getRoute(), getJourneyDate(), getDepartureTime(), getArrivalTime(), getFare());
}
}
BookingService.java
@Service
public class BookingService implements IBookingService {
@Autowired
private BookingRepository bookingRepository;
private AirCraftRouteRepository airCraftRouteRepository;
@Transactional
@Override
public Booking bookFlight(Booking booking){
// Long airCraftId = booking.getAirCraftRoute().getId().getAirCraftId().getAircraftId();
// Long routeId = booking.getAirCraftRoute().getId().getRouteId().getRouteId();
booking.setBookingReferenceNumber("##YTRJTG");
booking.setPnrNumber("PNRYTFRYHG");
int result = airCraftRouteRepository.updateAvailability();
System.out.println(result);
//bookingRepository.save(booking);
return booking;
}
}
AirCraftRouteRepository.java
@Repository
public interface AirCraftRouteRepository extends JpaRepository<AirCraftRoute,Integer> {
//@Lock(LockModeType.PESSIMISTIC_WRITE)
@Modifying
@Transactional
@Query(value = "UPDATE AirCraftRoute ar SET ar.availability = 5 where ar.fare = 2000",nativeQuery = true)
int updateAvailability();
}
Exception
@Query(value = "UPDATE AirCraftRoute ar SET ar.availability = 5 where ar.fare = 2000",nativeQuery = true)
int updateAvailability();
This is throwing null pointer exception. The error says:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.abc.gds.service.BookingService.bookFlight(BookingService.java:27) ~[main/:na]
Can someone please help?