Life-Pill / pharmacy-pos-main-backend

The Pharmacy POS System Backend is the server-side component of our comprehensive pharmacy management software. It is developed using Spring Boot, Java, Spring Security ,and Microservices Architecture
Apache License 2.0
5 stars 3 forks source link

Error Retrieving All Items - Null Value Assigned to Primitive Property - PREVIOUSLY WORKED FINE NOW NOT WORKING GIVES ERROR #95

Closed TheTharz closed 6 months ago

TheTharz commented 6 months ago

GitHub Issue: Error Retrieving All Items - Null Value Assigned to Primitive Property

Description

When attempting to retrieve all items from the database using the endpoint http://localhost:8081/lifepill/v1/item/get-all-items, an error occurs. The error message is as follows:

org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property [class com.lifepill.possystem.entity.Item.branchId] of primitive type setter of com.lifepill.possystem.entity.Item.branchId; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.lifepill.possystem.entity.Item.branchId] of primitive type setter of com.lifepill.possystem.entity.Item.branchId
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect...

This endpoint was previously working fine.

Steps to Reproduce

  1. Start the application.
  2. Make a GET request to http://localhost:8081/lifepill/v1/item/get-all-items.
  3. Observe the error in the response and the server logs.

Expected Behavior

The endpoint should return a list of all items without any errors.

Actual Behavior

An error is thrown indicating that a null value was assigned to a property of primitive type branchId in the Item entity.

Possible Cause

The error indicates that a null value is being assigned to a primitive type property (branchId) in the Item entity. Since primitive types in Java cannot be null, this results in an exception. This issue could be due to:

Suggested Fix

  1. Check Database Records: Verify that all records in the Item table have non-null values for the branchId column.
  2. Entity Mapping: Ensure that the branchId field in the Item entity is mapped correctly and consider using a wrapper type (Integer) instead of a primitive type (int) to allow for null values.
  3. Data Validation: Add validation logic to prevent null values for branchId when inserting or updating records in the database.

Logs

org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property [class com.lifepill.possystem.entity.Item.branchId] of primitive type setter of com.lifepill.possystem.entity.Item.branchId; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.lifepill.possystem.entity.Item.branchId] of primitive type setter of com.lifepill.possystem.entity.Item.branchId
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect...

Please address this issue as soon as possible, as it is affecting the retrieval of items from the database.

Thank you!

PramithaMJ commented 6 months ago

Current Behavior in Postman

When testing the endpoint http://localhost:8081/lifepill/v1/item/get-all-items, the following response is received in Postman:

{
    "code": 201,
    "message": "SUCCESS",
    "data": [
        {
            "itemId": 19,
            "brandId": 3,
            "itemName": "Item 9",
            "sellingPrice": 50.99,
            "itemBarCode": "901234567890",
            "supplyDate": "2024-04-11T18:30:00.000+00:00",
            "supplierPrice": 48.99,
            "itemManufacture": "Manufacturer I",
            "itemQuantity": 419.0,
            "itemCategoryName": "Vitamins and Supplements",
            "itemCategoryId": 4,
            "measuringUnitType": "GRAM",
            "manufactureDate": "2023-08-31T18:30:00.000+00:00",
            "expireDate": "2025-08-31T18:30:00.000+00:00",
            "purchaseDate": "2024-04-11T18:30:00.000+00:00",
            "warrantyPeriod": "9 years",
            "rackNumber": "I9",
            "discountedPrice": 49.99,
            "discountedPercentage": 50.0,
            "warehouseName": "Warehouse I",
            "itemImage": "item9_image.jpg",
            "itemDescription": "Description for Item 9",
            "stock": true,
            "freeIssued": true,
            "discounted": false,
            "specialCondition": false
        }
    ]
}

This response indicates that the endpoint works correctly under certain conditions.

Suggested Database Check

Please check the current state of the Item table in the database to ensure that the branchId field is not null for any records. If any records have a null branchId, this error will occur.

To check for null values in the branchId column, you can use the following SQL query:

SELECT * FROM Item WHERE branchId IS NULL;

ScreenShot

Screenshot 2024-05-19 at 2 19 09 AM

If any records are returned, update them to ensure that branchId has a valid non-null value. This should resolve the error.

Testing

  1. Verified that the Item table does not contain any null values for branchId.
  2. Tested the http://localhost:8081/lifepill/v1/item/get-all-items endpoint to ensure it returns the correct response without any errors.

This fix ensures that null values can be handled properly, preventing the exception and allowing the endpoint to return all items as expected.

Thank you!