0

I am building a spring rest API which will return promo information (promo_code, promo_description, efective_date,expiration_date) and I have the table structure like blow

PROMO_DETAIL SEQ_NO NUMBER(9), //PK PROMO_CODE VARCHAR2(9), //PK PROMO_DESCRIPTION VARCHR2(100), EFFECTIVE_DATE DATE, EXPIRATION_DATE DATE 

I am using an entity class having promo_code,promo_description,effective_date and expiration_date. I annotated only promo_code with @Id. I am directly returning the entity class as the response.

Problem is when there is 2 records with same promo code(but with different values of other fields). Though In this case JPA returns two records however both records have duplicate values for all fields.

Same type of question was asked here as well "JPA/Hibernate select query returning duplicate records"

Solution is to composite key in entity.

I do not want to add seq_no to entity class because it is not required in response. I also do not want to create unnecessarily separate response model class. It will create extra overhead of mapping entity to model class.

Is there any other solution to this problem?

7
  • Why have you annotated promo_code with @Id if two records can have same value for it? Commented Jun 10, 2018 at 10:13
  • @Jatin Hiberrnat does not allow an Entity without Id Commented Jun 10, 2018 at 10:23
  • The @Id annotation marks a field as a primary key field. As per ur description, promo_code doesn't sound like a primary key. Commented Jun 10, 2018 at 10:24
  • as the field cannot be individually used as a primary key, u should either find a field than can be used as one OR declare a composite key. Commented Jun 10, 2018 at 10:26
  • 1
    I do not want to add seq_no to entity class because it is not required in response: the HTTP response to some web service you're writing shouldn't drive the design of your entity. If you don't want to send it in the response, then just don't. But it must be in the entity, and must be part of its ID. (That said, not sending it in the response will make it impossible to expose a service that returns one promo_detail by ID, since the client will never know what its ID is) Commented Jun 10, 2018 at 11:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.