Home > Enterprise >  Unable to retrieve data from MySQL using Spring boot JPA
Unable to retrieve data from MySQL using Spring boot JPA

Time:11-04

I am getting a java.lang.NullPointerException error at the line where the program begins to retrieve data from the database, specifically starting with the code recordDB.setAccountName(billing.getAccountId().getAccountName());. The entity tables are joined together and at first I thought that it can't retrieve data from other other tables but I tried to run with just recordDB.setAmount(billing.getAmount()); Can someone explain what I missed or is there something wrong with the logic?

Component

@Component
public class FileProcessor {

    @Autowired
    private BillingRepository billingRepository;


    public FileProcessor() {

    }


    public List<Record> retrieveRecordfromDB(List<Request> requests) throws BarsException{

        List<Record> records = new ArrayList<>();

        if (!requests.isEmpty()) {
            for (Request request : requests) {

                Billing billing = billingRepository
                        .findByBillingCycleAndStartDateAndEndDate(
                                request.getBillingCycle()
                                , request.getStartDate()
                                , request.getEndDate());


                if (billing == null) {
                    throw new BarsException(BarsException.NO_RECORDS_TO_WRITE);
                }

                Record recordDB = new Record();

                recordDB.setBillingCycle(request.getBillingCycle());
                recordDB.setStartDate(request.getStartDate());
                recordDB.setEndDate(request.getStartDate());
                recordDB.setAccountName(billing.getAccountId().getAccountName());
                recordDB.setFirstName(billing.getAccountId().getCustomerId().getFirstName());
                recordDB.setLastName(billing.getAccountId().getCustomerId().getLastName());
                recordDB.setAmount(billing.getAmount());

                records.add(recordDB);
            }
        }
        return records;
    }

}

Account Entity

@Entity
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "account_id")
    private int accountId;

    private String accountName;
    private LocalDateTime dateCreated;
    private String isActive;
    private String lastEdited;


    public Account() {
    }

    public int getAccountId() {
        return accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public LocalDateTime getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(LocalDateTime dateCreated) {
        this.dateCreated = dateCreated;
    }

    public String getIsActive() {
        return isActive;
    }

    public void setIsActive(String isActive) {
        this.isActive = isActive;
    }

    public String getLastEdited() {
        return lastEdited;
    }

    public void setLastEdited(String lastEdited) {
        this.lastEdited = lastEdited;
    }

    public Customer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Customer customerId) {
        this.customerId = customerId;
    }

    public Set<Billing> getBilling() {
        return billing;
    }

    public void setBilling(Set<Billing> billing) {
        this.billing = billing;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "customer_id")
    private Customer customerId;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "account_id")
    private Set<Billing> billing;
}

Billing Entity

@Entity
public class Billing {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "billing_id")
    private int billingId;

    //private int billingId;
    private int billingCycle;
    private String billingMonth;
    private Double amount;
    private LocalDate startDate;
    private LocalDate endDate;
    private String lastEdited;
    //private Account accountId;

    public Billing() {
    }

    public int getBillingId() {
        return billingId;
    }

    public void setBillingId(int billingId) {
        this.billingId = billingId;
    }

    public int getBillingCycle() {
        return billingCycle;
    }

    public void setBillingCycle(int billingCycle) {
        this.billingCycle = billingCycle;
    }

    public String getBillingMonth() {
        return billingMonth;
    }

    public void setBillingMonth(String billingMonth) {
        this.billingMonth = billingMonth;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public LocalDate getStartDate() {
        return startDate;
    }

    public void setStartDate(LocalDate startDate) {
        this.startDate = startDate;
    }

    public LocalDate getEndDate() {
        return endDate;
    }

    public void setEndDate(LocalDate endDate) {
        this.endDate = endDate;
    }

    public String getLastEdited() {
        return lastEdited;
    }

    public void setLastEdited(String lastEdited) {
        this.lastEdited = lastEdited;
    }

    public Account getAccountId() {
        return accountId;
    }

    public void setAccountId(Account accountId) {
        this.accountId = accountId;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "account_id")
    private Account accountId;
}

Customer Entity

@Entity
public class Customer {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "customer_id")
    private int customerId;

    private String firstName;
    private String lastName;
    private String address;
    private String status;
    private LocalDateTime dateCreated;
    private String lastEdited;

    public Customer() {
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public LocalDateTime getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(LocalDateTime dateCreated) {
        this.dateCreated = dateCreated;
    }

    public String getLastEdited() {
        return lastEdited;
    }

    public void setLastEdited(String lastEdited) {
        this.lastEdited = lastEdited;
    }

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "customer_id")
    private Set<Account> account;

}

Repository

@Repository
public interface BillingRepository extends JpaRepository<Billing, Integer> {
    
    public Billing findByBillingCycleAndStartDateAndEndDate (int billingCycle, LocalDate startDate, LocalDate endDate);

}

CodePudding user response:

Are you sure the field names in the Billing class exactly match the database column names? I see you set the column name to "billing_id" explicitly for the id field, but not for any other fields. My guess is that the fields in that class are all null since there are no corresponding database columns (debug to confirm).

CodePudding user response:

  • Your naming is unintuitive, which hinders people unfamiliar with the code:

  • recordDB implies that it is the Database for records. Instead, it is a record that is to be saved in the DB. Naming it "RecordToSave" or similar is much better, since it gets the intention across.

  • getAccountId() implies that the id of an account is returned (an int or long) NOT that the account itself is returned. You should rename it to getAccount()

About the issue:

What you are using as a bidirectional ManyToOne <-> OneToMany relationship.

One side should be the owning side of the relationship. Here the @JoinColumn should be stated. The receiving end should have a MappedBy Property.

See this guide for more information: https://thorben-janssen.com/hibernate-tips-map-bidirectional-many-one-association/

It should solve the issue, since only the data retrieval for connected tables does not seem to work, hence fixing the references should fix the issue.

Your billing.getAmount() does refer to data written in the billing object/table, and is not from another table like billing.getAccountId().getAccountName() which gets data from the account table connected to the billings table.

Last, but not least:

Think about your cascading strategy. The way it currently works, deleting a billing will delete the account of that billing, which deletes all references made there and so on since you currently use Cascade.All for ALL entries. This is bad.

Here is a guide for cascading: https://howtodoinjava.com/hibernate/hibernate-jpa-cascade-types/

  • Related