JPA Mapping Basics Flashcards

1
Q

How do you identify a database entity?

A

Using the @Entity annotation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

@Entity

A

Specifies that the class is an entity. This annotation is applied to the entity class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the annotation elements for @Entity?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between:
@Entity(name=”A”)
@Table(name=”B”)

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the @Basic annotation used for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Where can @Basic be used?

A

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}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What elements can be used with @Basic?

A

FetchType fetch
Boolean optional
Example:
@Basic (fetch=FetchType.Eager, optional=true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

@Enumerated

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

@Temporal

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

transient or @Transient

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does bidirectional mean?

A

When each entity points to the other, the relationship is bidirectional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the directionality when each entity points to the other?

A

bidirectional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does unidirectional mean?

A

If only one entity has a pointer to the other, the relationship is said to be unidirectional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the directionality if only one entity points to the other?

A

unidirectional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Cartesian product?

A

TBD

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a tuple?

A

A tuple in the relational model is formally defined as a finite function that maps attributes to values. For example:

(player : "Harry", score : 25)
17
Q

COALESCE

A

we want to find out the best way to contact each person according to the following rules:

  1. If a person has a business phone, use the business phone number.
  2. If a person does not have a business phone and has a cell phone, use the cell phone number.
  3. 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;