June 2021

The best way to map @OneToOne


@OneToOne

The best approach to bidirectional @OneToOne is to use @MapsId

Student entity

@Entity
@Data
@Table(name = "students")
public class Student implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String email;

    @OneToOne(mappedBy = "student", cascade = CascadeType.ALL)
    private StudentDetails studentDetails;
}

StudentDetails entity

@Entity
@Data
@Table(name = "student_details")
public class StudentDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String studentDetailsInfo;

    @OneToOne
    @JoinColumn(name = "student_id")
    @MapsId
    private Student student;
}

The best way to map @ManyToMany


@ManyToMany

Many to many relationships can be easily mapped by creating another table using @JoinTable as follows.

Student entity

@Data
@Table(name = "Student")
@Entity
public class Student implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String studentName;

    @ManyToMany
    @JoinTable(
        name = "student_task",
        joinColumns = @JoinColumn(name = "task_id"),
        inverseJoinColumns = @JoinColumn(name = "student_id")
    )
    private Set<Task> tasks = new HashSet<>();
}


Task entity

@Entity
@Data
@Table(name = "tasks")
public class Task implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String taskName;

    @ManyToMany(mappedBy = "tasks")
    private Set<Student> students = new HashSet<>();
}

How to avoid NullPointerException


java.lang.NullPointerException

When can it be thrown?

According to the JavaDoc, the following scenarios can be found.
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

How to avoid it?

  • Use Optional in Java 8
  • Call equals() and equalsIgnoreCase() methods on known objects.
  • Use valueOf() over toString()
public class NullCheckDemo {
   public static void main(String args[]){
       Integer i = null;
       System.out.println(i.toString());      //This throws NullPointerException
       System.out.println(String.valueOf(i)); //This returns null
   }
}
  • Use null-safe methods and libraries. Ex: Apache Commons StringUtils.
  • Use @NotNull and @Nullable annotations
  • Always check for nulls.
public class NullDemo {
    public static void main(String args[]){
        Integer i = null;

        if(i != null)
            System.out.println(i);
        else
            System.out.println("error occurred");
    }
}


Thread pool in Java


The Thread pool

Introduction

  • Executor implementations use thread pools.
  • The thread pool is a collection of worker threads that are ready to serve.
  • Creating new threads and manage them uses a big amount of data.
  • Worker threads in the thread pool will help to reduce this overhead of creating threads.
  • Tasks are submitted to the pool via an internal queue called blocking queue.
  • If there are more tasks than active threads, these tasks will be inserted into a blocking queue until a thread becomes available.
  • A common type of thread pool is the fixed thread pool.

Fixed thread pool

  • This has a specific number of running threads.
  • If a thread stops, this will replace with a new thread.
  • The main advantage of the fixed thread pool is it will limit the threads of an application.
  • It won't let the application exceed the limit of threads that the application handles.

Java ExecutorService


Java ExecutorService

Introduction

  • If an application has few threads, it can be used threads very easily using the above methods.
  • But if an application has many threads, it will be not easy to handle this.
  • Executors can be used to avoid this complexity.
  • The executor framework is a framework that can be used for creating, managing and executing threads.
  • This provides an asynchronous feature to Java applications.

Features of Executor service

  • Thread creation
    • Provides thread pool and various methods to create threads.
  • Thread Management
    • The thread pool will manage the life cycle of threads.
  • Thread Execution
    • Various methods will be provided
    • Thread schedulers can be used.

Executor interfaces in the Java Concurrency API

  • Executor
    • Provides a single method execute() to create a thread.
    • (new Thread(r)).start() can be replaced with e.execute(r)
  • ExecutorService
    • Also provides execute() method but this accepts both Runnable and Callable objects.
  • ScheduledExecutorService
    • This can be used to act as the asynchronous manner in Java
    • It can be used periodically and after a specific timeout.
    • Runnable and Callable tasks can be executed


Thread priority


Thread priority

  • Each thread has a priority.
  • It starts from 1 to 10 and the highest priority is 10 while the lowest is 1.
  • The default priority of the main thread is 5.
  • If you set the thread priority out of 10, it will give a compile-time error
Exception in thread "main" java.lang.IllegalArgumentException

List vs Queue vs Set vs Map


 List vs Queue vs Set vs Map


ListQueueSetMap
DuplicatesYesYesNoNo(Allow duplicate values not keys)
OrderYesYesNoNo
Null valuesYesPriority queue doesn't allow, but queue using LinkedList allows nullSingle nullSingle null key and many null values

ArrayList vs LinkedList vs Vector


ArrayList vs LinkedList vs Vector 


ArrayList
LinkedList
Vector
Data structureIndex-based  dynamic arrayDoubly linked listGrowable array
Increment size50%No initial size100%
Traverse Uses iteratorUses iteratorUses enumeration
Memory usageLess memory usageMore memory usage
AccessibilityRandom and fastSequential and slowRandom and fast
OrderInsertion orderInsertion orderInsertion order
DuplicatesAllowAllowAllow
Insert / DeleteSlowFastSlow
SynchronizedNoNoYes
ImplementsRandomAccess interfaceNA
RandomAccess interface &
Serializable interface
Null valuesYesYesYes

Array vs ArrayList


 

Array
ArrayList
Fixed-sizeSize is not fixed
Not type-safeType-safe
Allow both primitives and objectsDoesn't allow primitives. But after Java 5 auto-boxing will convert primitives to objects

Type safety means the compiler will validate the types while compiling and throw an error if you do anything wrong. 

What is REST


REST

Introduction

  • Stands for Representational State Transfer.
  • REST is a web standards-based architecture that uses the HTTP protocol (port 80) for data communication.
  • Uses HTTP methods for data communication 
  • REST server simply provides access to resources and client access and presents the resource.
  • REST is stateless, so there is no session.
  • REST uses various representations like TXT, JSON, XML...etc to represent resources.

Methods in REST

  • POST
    • Sends data to the server for creating a new resource, maybe an entity. Often used when uploading a file or submitting a web form.
  • GET 
    • Retrieves data from the server. Should have no other effect.
  • PUT 
    • Similar to POST, but used to replace an existing entity.
  • PATCH
    • Similar to PUT, but used to update only certain fields within an existing entity.
  • DELETE 
    • Removes data from the server.
  • TRACE 
    • Provides a way to test what the server receives. It simply returns what was sent.
  • OPTIONS 
    • This allows a client to get information about the request methods supported by a service. 
    • The relevant response header is Allow with supported methods.
  • HEAD 
    • This returns only the response headers.
  • CONNECT 
    • Used by browser when it knows it talks to a proxy and the final URI begins with https://. 
    • The intent of CONNECT is to allow end-to-end encrypted TLS sessions, so the data is unreadable to a proxy.

Directives in Angular 11


 Directives

  • Directives are instructions in the DOM
  • It adds additional behaviors to the elements
  • Using inbuilt directives we can manage forms, lists, styles, and what users see.
  • There are a few types
    • Component directives
    • Attribute directives
    • Structural directives

Component directives

  • These directives are used in the main class.
  • Declared by @Component
Ex:
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

Attribute directives

  • Listen and modify the behavior of other HTML elements, attributes, properties, and components
    • NgClass
      • This allows to set the CSS classes dynamically.
    • NgStyles
      • This directive lets you to set a given DOM element's style properties.
<div [ngStyle]="{'background-color':'green'}"></<div>
      • Instead of this you can use following too.
[style.color]="getColor(person.country)"
    • NgModel
      • This bind inputs, select, text areas...etc and store the required user value in a variable.
      • Add two-way data binding
  • This allows to create new directives too.

Structural directives

  • These directives manipulate the DOM element or change the structure of the DOM element
  • These directives begin with *
  • Ex: *ngIf, *ngFor
<ul *ngFor="let age of ages">
    <li *ngIf="age > 20">
        {{age}}
    </li>
</ul>




Access token vs refresh token


Access token vs refresh token

Access token

  • This is short-lived
  • Send API request, with the access token
  • If the access token is invalid, fail and ask the user to re-authenticate
  • There are few types of access tokens
    • Bearer tokens
    • JWT tokens
    • Opaque token

Refresh token

  • These tokens are long-lived
  • Refresh tokens are used to retrieve access tokens
  • If the access token is invalid, try to update it using the refresh token
  • If the refresh request passes, update the access token and re-send the initial API request
  • If the refresh request fails, ask the user to re-authenticate

States of an object in Hibernate


Hibernate object states

There are three states
  • Transient 
    • If the object just created, no primary key and not associate with a session, then the object is in the transient state
  • Persistent 
    • If a session is open, the object is in the persistent state
  • Detached 
    • If a session is closed, the object is in the detached state

Streams vs Collections in Java


 Streams vs Collections


Collections
Streams
The collections are used to store & group data in data structures like List, Set, Map...etc.Streams are used to perform complex operations like filtering, matching, mapping, etc... on stored data
Data modification can be done. You can add or remove data from a CollectionData cannot be modified
External iterations need to be done Internal iterations are being used
Collections can be traversedStreams can be traversed only once. If you need another traverse, you need to create a new stream
Collections are eagerly constructedStreams are lazily constructed




Web server vs Application server


Web vs application servers

Introduction

  • The application server is the super one.
  • Each application server contains an inbuilt web server.
  • If you are not satisfied with the inbuilt server, you can add another web server to the application server.

 What is the difference?

Web serverApplication server
Ex: Tomcat, Jetty, ResinEx: Glassfish, Wildfly, JBoss, WebSphere
Not J2EE compatibleJ2EE compatible
Can run web applications onlyCan run both web and enterprise applications
Support for HTML, JSP, Servlets...Support for HTML, JSP, Servlets, EJB, JMS...etc as well.


Throw vs throws in exception handling


Throw vs throws



Throw
Throws
Uses inside a method when it is required to throw an exceptionThis is used in the method signature in which methods that exceptions can occur. 
Only one exception can be thrownThrows can be used to declare multiple exceptions
Used to handle unchecked exceptionsUsed to handle checked exceptions
Usually used to handle custom exceptionsThrows means, it says this method can be thrown these exceptions, when you use this method, you need to handle it.

 

Bean scopes in Spring Framework


Spring Bean scopes

Set up the scope

  • The @Scope annotation can be used
@Scope("singleton")
  • There are 6 scopes are available in the application context

singleton

  • This is the default scope.
  • The same object is returned every time.
  • Better to use for all stateless beans.

prototype

  • A new object is created every time.
  • Better to use for all stateful beans.

request

  • Here it will create a single instance and it will available in the complete lifecycle of an HTTP request
  • Only valid in ApplicationContext

session

  • Here it will create a single instance and it will available in the complete lifecycle of an HTTP session
  • Only valid in ApplicationContext

application

  • Here it will create a single instance and it will available in the complete lifecycle of the servlet context
  • Only valid in ApplicationContext

websocket

  • Here it will create a single instance and it will available in the complete lifecycle of the web socket
  • Only valid in ApplicationContext

Dependency injection in Spring Boot


Dependency injection


What is Dependency injection?

  • Dependency injection is a form of the IoC container
  • It is just passing dependencies to other objects or frameworks.
  • Dependency injection allows you to remove hardcoded dependencies and make applications loosely coupled, extendable and maintainable.
  • In Spring it uses two types of dependency injection methods, constructor and setter injection. In addition, we can use field injection as well.

Constructor injection

private final Flavor flavor;

Cake(Flavor flavor) {
    Objects.requireNonNull(flavor);
    this.flavor = flavor;
}
  • If there is a single construction, @Autowired annotation is optional after Spring 4.3. But if there is more than one constructor, we need to specify the constructor by mentioning it using @Autowired annotation.
  • Autowired field should be final
  • This method is immutable and reliable

Setter injection

private Topping toppings;

@Autowired
void setTopping(Topping toppings) {
    this.toppings = toppings;
}
  • More flexible
  • Objects are mutable
  • Null checks are required
  • Less secure than constructor injection, because dependencies can be overridden

Field injection

@Autowired
private Topping toppings;

  • Very easy to use, but not recommended. 
  • If you use this, the unit tests can be failed.

When to use Setter or Constructor injections

  • Constructor injection is better than Setter injection.
  • Use Setter injection when a number of dependencies are more or you need readability.
  • Use Constructor Injection when Object must be created with all of its dependencies.
  • We can change a value easily when using the setter injection. It is more flexible than constructor injection.
  • Setter injection will override the constructor injection. If we use both setter and constructor injection, the IOC container will use setter injection.

Why does constructor injection better than others?

  • All required dependencies are available at the initialization time.
  • This is very important in writing unit tests. Because it forces to load all objects for all dependencies.

Advantages of dependency injection

  • Easy unit testing
  • Configurable components
  • Separations of concerns
  • More control 

Git rebase vs git merge


rebase vs merge

Git rebase

  • When rebasing master branch will take all commits one by one as separate commits.
    • git fetch > git rebase origin/master

The golden rule of rebasing

  • Never use this on public branches

Advantages of rebasing

  • Rebase makes a linear order of commits.
  • Easy to read the history.

Disadvantages of rebase

  • Merge resolve conflicts in a single commit, but rebase, you will have to resolve one by one.

When to use rebase

  • If you need to commit one by one, not as a single commit.
  • When you need to remove a commit
  • When you need to reorder commits

Git merge

  • When we use git merge it will take all commits on the feature branch as one commit to the master branch.

Advantages of merging

  • Simple to use
  • Commit in the source branch are separated from other branches
  • The history will be in the graphical view

Disadvantages of merging

  • Complex to identify commits because of the graphical view.

jar vs war in Spring Boot


 Use jar not war

  • If you run mvn clean install in Spring Boot, you will get a fat jar file.
  • It contains everything like dependencies, embedded web server... etc.
  • You can just run java -jar jarFile in a JVM, the default is Tomcat embedded server or you can use Jetty.
  • You don't need any web server or application server to run spring boot jars.
  • Because spring boot will provide us a production-ready jar file with the embedded tomcat.
  • If you need to run spring boot applications in existing web servers, you may have to create war instead of a jar.
  • Spring Boot supports only Tomcat, Jetty, and Undertow as the embedded server

Java Garbage Collector


Garbage Collectors in Java

Cleanup in Java

  • GC can process only in the heap area
  • GC is running its own thread when it feels memory is running low

Types of GCs

  • Serial GC
  • Parallel GC
  • CMS (Concurrent Mark Swap) collector
  • G1 GC (Garbage First)
  • Z GC

Serial GC

  • Uses a single thread
  • Uses mark-copy for the young generation and mark-swap-compact for the old generation
  • When executing it freezes all other threads until GC finishes.
  • Efficient because no communication overhead between threads
  • Suitable for small applications
  • Use java -XX:+UseSerialGC to use this GC

Parallel GC

  • Same as Serial GC, but this has multiple threads to speed up the process.
  • Uses mark-copy for the young generation and mark-swap-compact for the old generation
  • This was the default GC until Java 8.
  • Suitable for multi-core machines and if you need better performance.
  • Use java -XX:+UseParallelGC to use this GC

CMS (Concurrent Mark Swap) collector

  • This is an improved and advanced version of parallel GC
  • Many threads are being used to scan heap memory
  • This is running using Mark and Swap algorithm
    • Marking live objects
    • Removing unreachable objects
  • How it works
    • Define GC roots. Ex. Active threads, local variables/ input variables of executing methods... etc.
    • Traverse through the object graph in the memory
    • The application thread must stop to traverse
    • This is called Stop The World pause
    • GC visited object marked as live
    • Now free up the memory, releasing unused memory. 
    • There are different ways to do this
      • Normal deletion
      • Deletion with compacting
      • Deletion with coping
  • This is deprecated in JDK 9
  • Use java -XX:+UseConcMarkSweepGC to use this GC

G1 GC (Garbage First)

  • Introduced in Java 7 to replace the CMS collector
  • Mostly used in multi-processor machines with a large amount of data
  • This divides the heap into several equal-sized heaps and collects the regions with lesser live data.
  • Most garbage-contained sections will be cleaned first.
  • Since Java 9, this is the default GC.
  • Use java XX:+UseG1GC to use this GC

Z GC

  • This is available since JDK 11
  • This performs all expensive works concurrently without stopping the execution.
  • Use java -XX:+UseZGC to use this GC

How to select a GC?

  • It is better to allow the virtual machine to select the GC
  • If necessary, adjust the heap size to improve performance.
  • If that is also not enough, select a GC as follows ,according to the official document
    • If the application has small data set (100MB) select Serial GC
    • If the application runs on a single processor and no pause time requirement, then select Serial GC
    • If the peak application performance is the first priority and there is no pause time then select Parallel GC
    • If the response time is more important, then select G1 GC
    • If the response time is the highest priority, then select Z1 GC

Possible causes for java.lang.OutOfMemoryError: Java heap space

  • Configuration issues (heap size may be not enough)
  • The application is holding objects without any usages
  • Excessive use of finalize() methods.