Lecture 4A Flashcards
What is a an embedded id?
Entire object in a field is used as primary key: @EmbeddedId DependentId id; A variant of composite key. Also original class must be @Embeddable
How to auto generate id
@Id @GeneratedValue
Int id;
How update existing entity?
em.merge(customer);
If there’s already a customer with same primary key merge updates it, otherwise it persists it. Must be embedded in transaction.
Explain process of em.find(customer.getClass(), id);
If class and id refers to an entity currently in PC this entity is returned from cache. If the entity is not in PC but is in database a new managed object is created in PC and returned. Otherwise null is returned.
How to write compound primary key in JPA?
@Id int id;
@Id String name;
JPQL ?
Java Persistence Query Language. Allows for more sophisticated queries. Based on older Hibernate Query Language. Operates on entities(objects) rather than tables.
Do basic JOQL search
SELECT c FROM Country c
Do JPQL queries need a transaction?
No. Example: Query myQuery = em.createQuery(“SELECT c FROM Country c”);
List results = query.getResultList();
How to make a query typed?
TypedQuery q = em.cresteQuery(“SELECT c FROM Country c”);
List results = q.getResultList();