JPA Mapping Basics Flashcards
How do you identify a database entity?
Using the @Entity annotation.
@Entity
Specifies that the class is an entity. This annotation is applied to the entity class.
What are the annotation elements for @Entity?
String name (Optional) The entity name. Defaults to the unqualified name of the entity class. This name is used to refer to the entity in queries. The name must not be a reserved literal in the Java Persistence query language.
What is the difference between:
@Entity(name=”A”)
@Table(name=”B”)
if you have a class @Entity(name="MyEntityName") @Table(name="MyEntityTableName") class MyEntity {}
then a table with name MyEntityTableName is created and the entity name is MyEntityName.
Your JPQL query would be :
select * from MyEntityName
What is the @Basic annotation used for?
Defines if a basic attribute can be lazy loaded and nullable. Default is Eagar loading and nullable (optional = true).
The simplest type of mapping to a database column. The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements java.io.Serializable.
Where can @Basic be used?
The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements java.io.Serializable.
@Basic
protected String name;
@Basic String getName() {return name}
What elements can be used with @Basic?
FetchType fetch
Boolean optional
Example:
@Basic (fetch=FetchType.Eager, optional=true)
@Enumerated
Used to define the type of value persisted for enum types. The default is ordinal but string can also be specified.
@Enumerated(javax.persistence.EnumType.STRING)
@Enumerated(javax.persistence.EnumType.ORDINAL)
Used with @Basic or @ElmentCollection where the collection is all basic types.
@Enumerated(javax.persistence.EnumType.STRING)
private EmployeeType previousType;
@Temporal
Used with fields or properties of type java.util.Date and java.util.Calendar to specify the type of value (TemporalType) which can be DATE, TIME, TIMESTAMP
transient or @Transient
Specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.
What does bidirectional mean?
When each entity points to the other, the relationship is bidirectional.
What is the directionality when each entity points to the other?
bidirectional
What does unidirectional mean?
If only one entity has a pointer to the other, the relationship is said to be unidirectional.
What is the directionality if only one entity points to the other?
unidirectional
What is a Cartesian product?
TBD
What is a tuple?
A tuple in the relational model is formally defined as a finite function that maps attributes to values. For example:
(player : "Harry", score : 25)
COALESCE
we want to find out the best way to contact each person according to the following rules:
- If a person has a business phone, use the business phone number.
- If a person does not have a business phone and has a cell phone, use the cell phone number.
- If a person does not have a business phone, does not have a cell phone, and has a home phone, use the home phone number.
We can use the COALESCE function to achieve our goal:
SELECT Name, COALESCE (Business_Phone, Cell_Phone, Home_Phone) Contact_Phone
FROM Contact_Info;