Create selects with Hibernate criteria Generico

Asked

Viewed 56 times

0

I’m making a web software, administrative, it has several graphs and several tables, the problem with that is that several graphs and tables generate many queries to the database, and with that, many classes that have a similar structure, for example, for graphs all have the date attribute, an array of integers or dates, I would like to write a more elegant code, making with only one repository class could bring the date values generically, I came to create a single class using java Generics that does basic operations for each entity of the database(select, update, delete, list...), it got better, works well and reduced me about 500 lines of code(are 18 entities...), I would like to do something like this for the dates.

Since I’m new to Ibernate, I’m finding it difficult to visualize how I can do this in a generic way, there may be an API that already does this... I’ll accept anything to improve the code. And of course, this is a good option? implement a generic class for all charts or a class for each?

  • I didn’t understand it very well, what would be your difficulty when creating this Generica class? It would be the Query?

  • the method will return an int Arry (int[]) only each graph has its own array, ie a single method, all arrays, I need to know which array to return... and in addition each select made within the database groups of different tables and different groupings and such... I want a single generic that brings the solution in a simple way and returns the array. It’s really confusing...

  • Have you thought about using interface? You can use it for Hibernate and each report can have an implementation.

  • I’ve been thinking about another way to do what you want and I’ve come up with this example: http://answall.com/a/173937/60946. See if it helps anything.

1 answer

1


There is the uaiCriteria, a Framework for JPA. I don’t think it’s a complete solution to your problem, but it’s an alternative to simplify the code.

For example, to get a list of Pessoa with the name José:

final UaiCriteria uaiCriteria = UaiCriteriaFactory.createQueryCriteria(entityManager, Pessoa.class);
uaiCriteria.andEquals("nome", "José");
final List uaiCriteriaResult = uaiCriteria.getResultList();
  • Pretty cool this API, but it still doesn’t solve the problem as you said, yet it’s the only answer, if nothing comes up, I’ll tag it as a solution, considering that it solves part of the problem...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.