0

I'm trying to return the record that I got from my database. But I'm having a problem on how I can do that because the data than I retrieved from the database is in a different class from the return parameter.

public List<Record> getRecord(List<Request> requests) { List<Record> records = new ArrayList<>(); for (Request request : requests) { Billing billing = billingRepository .findByBillingCycleAndStartDateAndEndDate( request.getBillingCycle() , request.getStartDate() , request.getEndDate()); if (billing != null) { // Need to add "billing" to "records" list here } } return records; } 

Record.class

public class Record { private int billingCycle; private LocalDate startDate; private LocalDate endDate; private String accountName; private String firstName; private String lastname; private double amount; public Record() { } //Getters and setters 

Billing.class

public class Billing { 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() { } //Getters and setters 

What can I do? and please explain the answer so I can understand it. I really want to learn

3
  • Are you allowed to add parameters billing Id and amount to Record, so that you don't need to have two separate classes Commented Jun 19, 2021 at 15:13
  • @sanjeevRm Not allowed to do that. I've searched some similar type of problems but it doesn't work for me. Commented Jun 19, 2021 at 15:17
  • 1
    just extend the record class like this: billing extends records, by extending record class you can add billing and record both into record list. Commented Jun 19, 2021 at 15:17

1 Answer 1

1

You can use DozerMapper. It will map the object to another object having same name properties or you have to write the mapping in the dozer-mapping xml.

Lets come to your question. Here you are trying to convert your entity to another object.

For that you have to write mapping code. It will be something like this and it is very common practice to convert entity objects to another object before using them.

Record toRecord(Billing billing) { if(billing == null) { return null; } Record record = new Record(); record.setBillingCycle = billing.getBillingCycle(); ... ... // other properties ... return record; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.