Friday, January 11, 2019

ClassLoaders in Java:
Class loaders can be implemented by implementing the abstract class ClassLoader. They provide functionalities to load classes using their names as parameters at runtime.In order to retrieve the systems default class loader we can do the following:
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
ClassLoader classClassLoader = ReflectableClass.class.getClassLoader();

Class reflectableClassInstanceLoaded = systemClassLoader.loadClass( "com.danibuiza.javacodegeeks.reflection.ReflectableClass" );

Wednesday, January 9, 2019

Hibernate N+1 Problem
--------------------------------
Let's say you have a collection of Car objects (database rows), and each Car has a collection of Wheel objects (also rows). In other words, Car -> Wheel is a 1-to-many relationship.
Now, let's say you need to iterate through all the cars, and for each one, print out a list of the wheels. The naive O/R implementation would do the following:
SELECT * FROM Cars;
And then for each Car:
SELECT * FROM Wheel WHERE CarId = ?
In other words, you have one select for the Cars, and then N additional selects, where N is the total number of cars.
Alternatively, one could get all wheels and perform the lookups in memory:
SELECT * FROM Wheel
This reduces the number of round-trips to the database from N+1 to 2. Most ORM tools give you several ways to prevent N+1 selects.
How to avoid n+1 select:
https://dzone.com/articles/how-identify-and-resilve-n1
Java8:
The implementation of Hashmap tries to mitigate this by organising some buckets into trees rather than linked lists if the buckets becomes too large.