Kuba Hejda
(Lombok, JPA, spring-data-jpa, DTO)
consists of four areas:
The Java Persistence API
The query language
The Java Persistence Criteria API
Object/relational mapping metadata
| EntityManagerFactory | This is a factory class of EntityManager. It creates and manages multiple EntityManager instances. |
| EntityManager | It is an Interface, it manages the persistence operations on objects. It works like factory for Query instance. |
| Entity | Entities are the persistence objects, stores as records in the database. |
| EntityTransaction | It has one-to-one relationship with EntityManager. For each EntityManager, operations are maintained by EntityTransaction class. |
| Persistence | This class contain static methods to obtain EntityManagerFactory instance. |
| Query | This interface is implemented by each JPA vendor to obtain relational objects that meet the criteria. |
| @Entity | Specifies to declare the class as entity or a table. |
| @Table | Specifies to declare table name. |
| @Basic | Specifies non constraint fields explicitly. |
| @Embedded | Specifies the properties of class or an entity whose value instance of an embeddable class. |
| @Id | Specifies the property, use for identity (primary key of a table) of the class. |
| @GeneratedValue | Specifies, how the identity attribute can be initialized such as Automatic, manual, or value taken from sequence table. |
| @Transient | Specifies the property which in not persistent i.e. the value is never stored into database. |
| @Column | Used to specify column or attribute for persistence property. |
| @SequenceGenerator | used to define the value for the property which is specified in @GeneratedValue annotation. It creates a sequence. |
| @TableGenerator | Used to specify the value generator for property specified in @GeneratedValue annotation. It creates a table for value generation. |
| @AccessType | Used to set the access type. If you set @AccessType(FIELD) then Field wise access will occur. If you set @AccessType(PROPERTY) then Property wise assess will occur. |
| @JoinColumn | Used to specify an entity association or entity collection. This is used in many- to-one and one-to-many associations. |
| @UniqueConstraint | Used to specify the field, unique constraint for primary or secondary table. |
| @ColumnResult | References the name of a column in the SQL query using select clause. |
| @ManyToMany, @ManyToOne, @OneToMany, @OneToOne |
Used to define a many-to-many relationship between the join Tables. |
| @NamedQueries | Used for specifying list of named queries. |
| @NamedQuery | Used for specifying a Query using static name. |
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
// ...
}Bean contains the default constructor.
Non-Boolean property contains getter and setter methods.
Boolean property contain setter and is method.
Getter method of any property should start with small lettered ‘get’ (java method convention).
Setter method of any property should start with small lettered ‘set’ (java method convention).
Query query = entitymanager.createQuery("Select UPPER(e.ename) from Employee e ORDER BY e.ename ASC");
List<String> list = query.getResultList();EntityManager em = ...;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
Root<Country> country = cq.from(Country.class);
cq.select(country);
TypedQuery<Cuntry> q = em.createQuery(cq);
List<Country> allCountries = q.getResultList()<!-- https://mvnrepository.com/artifact/org.springframework.boot
/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.0</version>
</dependency>
#application.properties
#spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://localhost/ita2022
spring.datasource.username=ita2022
spring.datasource.password=ita2022