2

I need to find an object using two fields of an embedded key

Here is the embedded key:

public class OrderItemId implements Serializable {

    private static final long serialVersionUID = 1163347452811191867L;

    @Column(name = "order_code", length = 25)
    private String orderCode;

    @Column(name = "barcode", length = 25)
    private String barcode;

    // ....
}

Here is the class of the object I want to query:

@Entity
@Table(name = "order_item")
public class OrderItem {

    @EmbeddedId
    @NotNull
    private OrderItemId id;

    @Column(name = "quantity")
    private Integer quantity;

    @Column(name = "price")
    private Double price;

    // ...
}

As in this StackOverflow Answer

To query by embedded key orderCode, I can write something like this

public List<OrderItem> findById_OrderCode(String orderCode);

and it works!

But I don't know how to query by both orderCode and barcode. I have tried some forms of and but no use.

2
  • Please put your answer in a proper answer instead of putting it in the question. This will let others see that the question is answered. Commented Feb 22, 2019 at 7:22
  • Thanks, I'll update as your suggestion. Commented Feb 23, 2019 at 2:12

1 Answer 1

6

Never mind, I have figured out the query, it is

public OrderItem findById_OrderCodeAndId_Barcode(String orderCode, String barcode);
Sign up to request clarification or add additional context in comments.

Comments

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.