Viktor Martiš
(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(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String number;
@OneToOne
@JoinColumn(name = "details_id")
private PhoneDetails details;
public Phone() {
}
...@Entity
public class Trainer {
@OneToMany
@JoinTable(
name="TrainedMonkeys",
joinColumns = @JoinColumn( name="trainer_id"),
inverseJoinColumns = @JoinColumn( name="monkey_id")
)
public Set<Monkey> getTrainedMonkeys() {
...
}
@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;
@Column(name = "pswd")
@ColumnTransformer(
read = "decrypt( 'AES', '00', pswd )",
write = "encrypt('AES', '00', ?)"
)
private String password;
@ManyToMany(mappedBy = "employees")
private List<Project> projects = new ArrayList<>();
...
}
@Entity(name = "Project")
public class Project {
@Id
private Long id;
@ManyToMany
private List<Employee> employees = new ArrayList<>();
...
}@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( );