JavaBeans
, or with fields (field), that is, class variables (instance variables). Both types of elements of the Entity class are called attributes of the Entity class. findById()
is available in CrudRepository while getOne()
is available in JpaRepository. The findById()
returns null if record does not exist while the getOne()
will throw an exception called EntityNotFoundException
. one-to-one
or one-to-many
mapping is removed from the mapping, then remove operation can be cascaded to the target entity. Such target entities are known as orphans, and the orphanRemoval attribute can be used to specify that orphaned entities should be removed. Mapped Superclass
is a class from which Entity is inherited, it may contain JPA
annotations, however such a class is not Entity, it does not have to fulfill all the requirements set for Entity (for example, it may not contain a primary key). Such a class cannot be used in EntityManager
or Query operations. Such a class should be marked with the MappedSuperclass
annotation or, respectively, described in the xml
file. all Entities
: close, isOpen, getProperties, setProperty, clear.CrudRepository
provides CRUD
functions. PagingAndSortingRepository
provides methods to do pagination and sort records.JpaRepository
provides JPA
related methods such as flushing the persistence context and delete records in a batch.@Entity
public class Order {
@ElementCollection
protected List orderLines = new ArrayList<>();
}
javax.persistence.ElementCollection
has an attribute fetch which can be specified with two values either 'LAZY' or 'EAGER'//Load order lines eagerly
@Entity
public class Order {
@ElementCollection(fetch=EAGER)
protected List orderLines = new ArrayList<>();
}
@GeneratedValue annotation
:@SequenceGenerator
annotation.(FIELD)
or class properties (PROPERTY)
that have getters (getter)
and setters. MappedSuperclass
.subclasses
have their own database tables and fetching a subclass entity requires a join with the parent table as well. See Joined table.findAll()
method : It retrieves all the saved entities.findById(ID id)
method : It retrieves the entity by ID given as input.findAllById(Iterable<ID> ids)
: It retrives the list of entities which matches the list of input Ids.existsById(ID id)
: It returns true or false, depending upon the entity with input Id exists or not in the database.save(Entity entity)
: To save the entity and similarly saveAll(Iterable<ENTITY>
entities) to save the list of entities.delete(Entity)
, deleteById(ID)
, deleteAll()
, and deleteAll(Iterable<ID>
ids) methods are also there but it is rarely used by a developer in a project in comparison to other method. Hence, skip these and tell only if interviewer asks them explicitly.case-sensitive*
). After that we need to give the Entity property name by which we want to fetch the result.String name;
, and we declare the method findByName
, then it will throw an exception.saveAll()
just calls save()
in an iterative fashion.saveAll
method is annotated with @Transactional
so it’s either all or none will be saved.deleteAll
calls delete method iteratively, deleteAllInBatch
method calls executeUpdate.deleteAll()
will triggers 100 SQL queries
while deleteAllInBatch()
will trigger just one. This is a very important distinction, performance wise.Criteria API
is a specification that provides type-safe and portable criteria queries written using Java programming language APIs. It is one of the most common ways of constructing queries for entities and their persistent state. It is just an alternative method for defining JPA queries. Criteria API defines a platform-independent criteria queries, written in Java programming language. It was introduced in JPA 2.0
. The main purpose behind this is to provide a type-safe way to express a query.