Exposing Hibernate criteria via service API

前端 未结 6 1153
鱼传尺愫
鱼传尺愫 2021-02-04 14:14

This is more of a design than implementation question and it\'s going to be long so bear with me. It\'s best explained with an example:

Let\'s say I have a business

6条回答
  •  被撕碎了的回忆
    2021-02-04 14:41

    Hibernate is a low-level infrastructure framework, and as such should remain hidden behind the scenes. If next month your application needs to switch to another ORM framework for some reason, your API will be useless. Encapsulation even between layers within the same application, is vitally important.

    Having said all this, I think your method should receive an abstraction of the information you need to perform the search. I advise you to create an enum of Product's fields, and implement one or two simple versions of Restriction.

    The parameters to the method can be a list of equality restrictions, another list of relative restrictions and of course an order indicator (one of the enum values plus a flag for asc/desc).

    This is just a general direction, I hope I made my point clear =8-)

提交回复
热议问题