@Size vs @Length vs @Column

@Size and @Length are similar which are used to validate the size of a field. The first is a Java-standard annotation and the second is specific to Hibernate. Whereas @Column, is a JPA annotation that we use to control DDL statements.

Example for @Size
This annotation makes the bean independent of JPA and its vendors such as Hibernate

public class Customer {
    ...
    @Size(min = 5, max = 15)
    private String fullName;
    ...
}

Example for @Length
It is Hibernate-specific version of @Size

@Entity
public class Customer {
    …
    @Length(min = 5, max = 15)
    private String fullName;
    …
}

Example for @Column
It is used to indicate specific characteristics of the physical database column.

@Entity
public class Customer {
    …
    @Column(length = 15)
    private String fullName;
    …
}

JPA EntityManagerFactory : A Quick Overview

The main role of an EntityManagerFactory instance is to support instantiation of EntityManager instances. An EntityManagerFactory is constructed for a specific database, and by managing resources efficiently (e.g. a pool of sockets), it provides an efficient way to construct multiple EntityManager instances for that database.

Step 1: Creating an Entity Manager Factory Object:- ‘Persistence’ is a bootstrap class, which is used to obtain the reference for ‘EntityManagerFactory’ interface.

 
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");

Step 2: Obtaining an Entity Manager From a Factory:- By calling a createEntityManager() method on entityManagerFactory we can have a new application-managed EntityManager

 
EntityManager entityManager = entityManagerFactory.createEntityManager();

Step 3: Intializing an Entity Manager:- Resource-level EntityTransaction object could be obtained by calling getTransaction method

 
entityManager.getTransaction().begin();

Step 4: Persisting a Data Into the Relational Database:- An entity is passed within persist method to make an instance managed and persistent.

 
entityManager.persist(employeeObject);

Step 5: Closing the Transaction & Releasing the Factory Resources:- Methods commit and close are used to close the transaction and release the factory resources respectively

 
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();

Brief Code Snippet:-

 
public void addEntity() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    Employee employeeObject = new Employee("Vinay", "Chauhan");
    entityManager.persist(employeeObject);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}