java

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

The best way to map @OneToOne


@OneToOneThe best approach to bidirectional @OneToOne is to use @MapsIdStudent entity@Entity@Data@Table(name = "students")public class Student implements Serializable {     @Id     @GeneratedValue     private Integer id;     private...

The best way to map @ManyToMany


@ManyToManyMany to many relationships can be easily mapped by creating another table using @JoinTable as follows.Student entity@Data@Table(name = "Student")@Entitypublic class Student implements Serializable {     @Id     @GeneratedValue(strategy = GenerationType.AUTO)...

How to avoid NullPointerException


java.lang.NullPointerExceptionWhen 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...

Thread pool in Java


The Thread poolIntroductionExecutor 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...

Java ExecutorService


Java ExecutorServiceIntroductionIf 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...

States of an object in Hibernate


Hibernate object statesThere are three statesTransient If the object just created, no primary key and not associate with a session, then the object is in the transient statePersistent If a session is open, the object is in the persistent stateDetached If a session is closed, the object...

Throw vs throws in exception handling


Throw vs throwsThrowThrowsUses 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 exceptionsUsed to handle unchecked exceptionsUsed to handle...

jar vs war in Spring Boot


 Use jar not warIf 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...