Where should I use Hibernate criteria in MVC architecture?

I am working on a CRM project that uses Spring MVC and Hibernate, and I do not know what is the best place to use the sleep criteria. I want to use the criteria for sleep mode, because we have search capabilities at our presentation level, and users can search for a variety of different parameters in different ways. Sometimes we only need identifiers, sometimes we need a subset of properties, sometimes we need to join several tables, etc. So, building structured criteria, such as sleeping criteria, instead of passing a list of parameters, orders, required parameters, and search limits from the layer view to the data layer, can clear the code. However, I know that it is wrong to use hibernation at the presentation level, since it is against the MVC architecture. And I really do not think that duplicating the criteria for sleep mode is the right approach. I could think of 3 approaches:

  • Create a dozen methods in the business layer, one for each type of search query, and call each of these functions from the presentation level based on the situation. Each of these methods basically does nothing but pass parameters to the corresponding DAO method, which would create an SQL query (or criterion object) and retrieve data from the database. In this approach, I get hundreds of methods that do nothing but pass DAO parameters.

  • Creating a class similar to the Hibernate Criteria class in a view (or business layer). Then it initiates this object with the search parameters in the presentation layer and passes it to the DAO. The DAO then creates a sleep criteria criteria object based on this object. This approach involves duplication of the sleep criteria class.

  • Initiate the Hibernate criteria class in the presentation layer and pass its DAO to get the search result.

Could you tell me which one works best?

thanks

+4
source share
3 answers

I think the best choice depends on your requests.

If possible, I recommend you switch to the first alternative. I often find that I implement DAO lookup methods that take many parameters with a null value. The DAO method itself creates restrictions on adding criteria objects if the corresponding method parameters are not set to NULL.

This is a simple example:

public List<SomeObject> findSomeObjects(String name, Integer categoryId, Date dateTimeFrom, Date dateTimeUntil) { if (name != null) // add name to criteria if (categoryId != null) // add category to criteria // ... } 

If there really are many different search operations, and the number of combinations is very large, you can also try the second alternative. Perhaps you can limit your clone "Clone" by simplifying and adapting it for your use cases.

+1
source

Another option is to create a query-specific layer. This concept comes from CQRS. I do not use NHibernate, but I use ADO.NET to execute my requests directly, as I subscribe to the idea that the domain model should not be requested. There is nothing wrong with loading a complete aggregate here and there, but definitely not for special requests.

So, hypothetically, you could have something like ContactQuery with methods such as:

  • public string Name(Guid contactId)
  • public DataRow Details(Guid contactId)
  • public DataTable CustomerContacts(Guid customerId)

Thus, your requests are abstracted. We hope that NHibernate projections return a DataRow / DataTable :)

+1
source

I would go with the creation and transfer of Criteria objects to the DAO level. The reason is twofold:

  • to prevent a natural explosion of DAO search methods
  • to avoid duplication of code (which is inherent in the second option).

Thus, you can consider criteria objects as part of a vertical domain.

PS I donโ€™t know your situation, but a slightly better option is to use the JPA 2 criteria as a standard โ€œalternativeโ€ for Hibernate criteria to avoid dependence on Hibernate features without any special need.

0
source

All Articles