Thursday, November 28, 2019

Removing duplicate records from a hibernate query, while keeping pagination in mind

I was faced with a task, where I had to remove duplicate records from the result of a hibernate query, such that pagination parameters had to be applied only after the duplicate records were removed.

The following code snippet, used a single criteria object to do that.

https://stackoverflow.com/questions/11038234/pagination-with-hibernate-criteria-and-distinct-root-entity/23618190#23618190

What you want for each pagination page is 2 things, the total count of all results and your single page of results, but to do that you need to take 3 steps. 1) get the total count, 2) get the unique ids for your page, and 3) get the full data for the ids found in step 2. And you can do all that with a single criteria object:
1) get total count, using distinct ids (uniqueField = the name of your id in the entity class)
  Criteria criteria = session.createCriteria(YourEntity.class);
  Projection idCountProjection = Projections.countDistinct(uniqueField);
  criteria.setProjection(idCountProjection);
  //setup criteria, joins etc here
  int totalResultCount = ((Long)criteria.uniqueResult()).intValue();
2) reset projection and set start and length (you want the distinct ids)
  criteria.setProjection(Projections.distinct(Projections.property(uniqueField)));
  criteria.setFirstResult(start); 
  criteria.setMaxResults(length);
  List uniqueSubList = criteria.list();
3) reset projection and get distinct results that match ids

  criteria.setProjection(null);
  criteria.setFirstResult(0); criteria.setMaxResults(Integer.MAX_VALUE);
  criteria.add(Restrictions.in(uniqueField, uniqueSubList));
  criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
  List searchResults = criteria.list();
  //and now, however you want to return your results
  Map<String, Object> searchResultsMap = new HashMap<String, Object>();
  searchResultsMap.put("searchResults", searchResults);
  searchResultsMap.put("totalResultCount", totalResultCount);

Friday, September 20, 2019

Javascript debugging made easy


1) console.trace()
1.1)  Use console.trace() to figure out the entire stack trace from where a function call was made

2) console.group(label)
2.1) Use console.group(label) and console.groupEnd() to group the log statements together.
Be sure to specify the label i.e. the group name, so that it is easily readible amongst the verbose browser output.

3) Conditional Breakpoints
3.1) USE CONDITIONAL breakpoints! Makes life so much simpler.

4) Console.dir instead of console.log

May 31, 2019 - The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects. In other words, console.dir() is the way to see all the properties of a specified JavaScript object in console by which the developer can easily get the properties of the object.

5) Use console.table() to output an object in tabular format to the console.
Also, here are other super useful articles :
https://medium.com/appsflyer/10-tips-for-javascript-debugging-like-a-pro-with-console-7140027eb5f6


https://medium.com/datadriveninvestor/stopping-using-console-log-and-start-using-your-browsers-debugger-62bc893d93ff

https://medium.com/@mattburgess/beyond-console-log-2400fdf4a9d8

https://medium.com/better-programming/boost-your-javascript-debugging-skills-with-these-console-tricks-ab984c70298a

https://medium.com/better-programming/how-to-find-bugs-in-your-code-with-the-debugger-a7f739ea98

6) Turn on the view timestamp setting in the browser console.
7) Use console.error() and console.warn() instead of console.log() so the logged content is easily identifiable because it will be displayed in red and yellow colour respectively.