0

I am trying to map an array of Objects to a field. All the fields in that object are being mapped to columns with different name but similar structure. The response structure should be:

 "customers": [
    {
      "firstName": "string",
      "lastName": "string",
      "products": [
        {
            "description":"string",
            "amount": "string"
        },
        {
            "description":"string",
            "amount": "string"
        }
      ]
    }
 ]

Inside the products field, I have a list of product(description and amount). In DB, columns are stored like

product_des1,product_amt1,product_des2,product_amt2.....product_des30,product_amt30

. I need to map these two fields to the product(object). How should I approach to solve the problem using JPA annotations if possible?

For the reference: Customers.class

@Entity
public class Customers implements Serializable {

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

    @ElementCollection
    List<Products> products;

}

Product.class

@Embeddable
public class Product implements Serializable {

    @Column(?)
    private String description;

    @Column(?)
    private String amount;

}
8
  • Just have a look into JPA spec. Commented Nov 15, 2019 at 11:23
  • @Zorglube I checked but did'nt find anything specific to this scenario. Commented Nov 15, 2019 at 11:28
  • So you have 1 table for both entities? And you can have a max of 30 products? Commented Nov 15, 2019 at 11:34
  • 2
    I would start by hunting down whoever designed the database. Commented Nov 15, 2019 at 13:13
  • 1
    In that case, Smutje's answer is the way to go. Though it would probably make more sense to redesign the database and set up a proper @ManyToMany relationship. This is currently not possible Commented Nov 15, 2019 at 13:56

1 Answer 1

1

Inside the products field, I have a list of product(description and amount). In DB, columns are stored like

product_des1,product_amt1,product_des2,product_amt2.....product_des30,product_amt30

So your Products JPA entity should simply look like this:

@Embeddable
public class Products implements Serializable {

    @Column(name = "product_des1")
    private String description1;

    @Column(name = "product_amt1")
    private String amount1;

    @Column(name = "product_des2")
    private String description2;

    @Column(name = "product_amt2")
    private String amount2;

   // ... repeat
}

if you don't want to do additional mapping between the DB and JPA entities (which I don't recommend - I try to keep JPA entities as exact representation of a DB row and map, if necessary, in Java and not between different technologies).

Sign up to request clarification or add additional context in comments.

3 Comments

But using this way I'll be having several fields, it should be "description" and "amount". Moreover is it advisable to solve it programatically?
I strongly recommend to keep the database and the JPA entities equal, i.e. let the JPA entities reflect the DB 1:1 because having both a different technology (database vs. Java class) and representation (DB structure vs. object representation) is a guaranteed way to confusion and less maintainability because you have to keep multiple things in order when changing one or the other. I don't see any problem in having a proper object with a list of products as the "business" representation of your application and would do a mapping between both in a service, or simply adjust the database.
Oh okay , I'll try to keep my products class as suggested by you then later I'll manipulate while sending the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.