Which two mapping options can be chosen? (Choose two.)

A developer is creating an entity which is mapped to a table that has a primary key constraint defined on two character columns and would like to use mapping defaults as much as possible to simplify the code.
Which two mapping options can be chosen? (Choose two.)

A developer is creating an entity which is mapped to a table that has a primary key constraint defined on two character columns and would like to use mapping defaults as much as possible to simplify the code.
Which two mapping options can be chosen? (Choose two.)

A.
Use an @id property that constructs a private field as a concatenation of two columns.

B.
Use a separate class to map those two columns and use an @idclass annotation to denote I primary key field or property in the entity.

C.
Use a separate @Embeddable class to map those two columns and use an @EmbeddedId annotation to denote a single primary key field or property in the entity.

D.
Use a separate @Embeddable class to map those two column and add two fields or properties the entity, each marked as @id, that correspond to the fields or properties in the embeddable
class.

E.
Use a separate class to map those two columns. Specify that class using @Idclass annotation on the entity class. Add two fields or properties to the entity, each marked as @Id, that correspond to the fields or properties in that separate class.



Leave a Reply 5

Your email address will not be published. Required fields are marked *


Tommy_Croatia_ZGB

Tommy_Croatia_ZGB

C: An entity that contains a single field of the same type as the primary key class is said to use an embedded id class. The embedded id class is just an embedded object that happens to be composed of the primary key components. We use an @EmbeddedId annotation to indicate that it is not just a regular embedded object but also a primary key class.
Like other embedded objects, the embedded id class must be annotated with @Embeddable.
Using the embedded primary key class is no different than using a regular embedded type, except that the annotation used on the attribute is @EmbeddedId instead of @Embedded.

Example:

@Embeddable
public class EmployeeId {
private String country;
@Column(name=”EMP_ID”)
private int id;

public EmployeeId() {}

public EmployeeId(String country, int id) {
this.country = country;
this.id = id;
}

// …
}

@Entity
public class Employee {
@EmbeddedId private EmployeeId id;
private String name;
private long salary;

public Employee() {}

public Employee(String country, int id) {
this.id = new EmployeeId(country, id);
}

public String getCountry() { return id.getCountry(); }
public int getId() { return id.getId(); }
// …
}

E: The first and most basic type of primary key class is an id class. Each field of the entity that makes up the primary key is marked with the @Id annotation. The primary key class is defined separately and associated with the entity by using the @IdClass annotation on the entity class definition.

Example:

@Entity
@IdClass(EmployeeId.class)
public class Employee {
@Id private String country;
@Id
@Column(name=”EMP_ID”)
private int id;
private String name;
private long salary;
// …
}

public class EmployeeId implements Serializable {
private String country;
private int id;

public EmployeeId() {}
public EmployeeId(String country, int id) {
this.country = country;
this.id = id;
}

public String getCountry() { return country; }
public int getId() { return id; }

public boolean equals(Object o) {
return ((o instanceof EmployeeId) &&
country.equals(((EmployeeId)o).getCountry()) &&
id == ((EmployeeId)o).getId());
}

public int hashCode() {
return country.hashCode() + id;
}
}