Kuba Hejda
(flyway, JPA - advance, DTO)
| 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. |
| find(Class<T> entityClass, Object primaryKey) | Find by primary key. |
| getReference(Class<T> entityClass, Object primaryKey) | Get an instance, whose state may be lazily fetched. |
| persist(Object entity) | Make an instance managed and persistent. |
| merge(T entity) | Merge the state of the given entity into the current persistence context. |
| remove(Object entity) | Remove the entity instance. |
| refresh(Object entity) | Refresh the state of the instance from the database, overwriting changes made to the entity, if any. |
| flush() | Synchronize the persistence context to the underlying database. |
| createQuery(String qlString) | Create an instance of Query for executing a Java Persistence query language statement. |
| createNamedQuery(String name) | Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL). |
@MappedSuperclass
public class AbstractEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "serial")
@Id
private Long id;@Entity
@Inheritance( strategy = InheritanceType.JOINED)
public class User {
//unidirectional example
@Entity
public static class Classroom {
@Id
@GeneratedValue
private Long id;
private String number;
@OneToOne
@JoinColumn(name = "classroom_id")
private Blackboard blackboard;
...@Entity
public class Classroom {
@Id
public Long id;
@OneToMany
@JoinTable(
name="ClassroomStudents",
joinColumns = @JoinColumn( name="classroom_id"),
inverseJoinColumns = @JoinColumn( name="student_id")
)
public Set<Student> students;
}
@Entity
public class Student {
@Id
public Long studentId;
}@Entity
public class Classroom {
@Id
public Long id;
@OneToMany
@JoinColumn(
name = "classroom_id",
referencedColumnName = "id"
)
public Set<Student> students;
}
@Entity
public class Student {
@Id
public Long studentId;
}@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
public Person() {
}
}
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String number;
@ManyToOne
@JoinColumn(name = "person_id",
foreignKey = @ForeignKey(name = "PERSON_ID_FK")
)
private Person person;
...
}@Entity(name = "Employee")
public static class Employee {
@Id
private Long id;
@ManyToMany
@JoinTable(
name = "r_employee_project",
joinColumns = @JoinColumn(name = "id_employee"),
inverseJoinColumns = @JoinColumn(name = "id_project")
)
private List<Project> projects = new ArrayList<>();
...
}
@Entity(name = "Project")
public class Project {
@Id
private Long id;
...
}@Entity
@Table
@NamedQuery(query = "Select e from Employee e where e.eid = :id", name = "find employee by id")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int eid;
private String ename;
private double salary;
private String deg;Query query = entitymanager.createNamedQuery("find employee by id");
query.setParameter("id", 1204);
List<Employee> list = query.getResultList( );
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>