Which one of the pairs of classes below correctly achieves this task?

The developer wants to define a unidirectional relationship from the customer entity to the order entity and map this relationship using a foreign key mapping strategy.
Which one of the pairs of classes below correctly achieves this task?

The developer wants to define a unidirectional relationship from the customer entity to the order entity and map this relationship using a foreign key mapping strategy.
Which one of the pairs of classes below correctly achieves this task?

A.
@Entity public class Customer {
@Id int customerId;
@OneToMany @JoinColumn (name = �CUST_ID�) Set <Order> orders;
. . .
}
@Entity public class order {
@Id int orderId;
. . .
}

B.
@Entity public class Customer {
@Id int customerId;
@OneToMany Set <Order> orders;
. . .
}
@Entity
@JoinColumn (names = �CUST-ID�, referencedColumnName = �customerId�)
public class order {
@Id int order Id;
. . .
}

C.
@Entity public class Customer {
@Id int customerId;
@OneToMany (JoinColumn = @joinColumn (name = �CUST_ID�) Set <Order> orders;
. . .
}
@Entity public class order {
@Id int orderId;
. . .
}

D.
@ Entity public class Customer {
@Id int customerId;
@OneToMany (JoinColumn = @JoinColumn (name = �CUST_ID�), table = ��ORDER) Set
<Order> orders;
. . .
}
@Entity public class order {
@Id int orderId;
. . .
}



Leave a Reply 4

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


Mohamed Fayek

Mohamed Fayek

unidirectional OneToMany :
@OneToMany
@JoinColumn(name=”TXTHEAD_CODE”)
private Set text;

Mohamed Fayek

Mohamed Fayek

A is correct

Lejmi Salah

Lejmi Salah

A is correct.

@Target(value = {ElementType.METHOD, ElementType.FIELD}) // …
public @interface OneToMany {
public Class targetEntity() default void.class;
public CascadeType[] cascade() default {};
public FetchType fetch() default FetchType.LAZY;
public String mappedBy() default “”;
public boolean orphanRemoval() default false;
}

@Target(value = {ElementType.METHOD, ElementType.FIELD}) // …
public @interface JoinColumn {
public String name() default “”;
public String referencedColumnName() default “”;
public boolean unique() default false;
public boolean nullable() default true;
public boolean insertable() default true;
public boolean updatable() default true;
public String columnDefinition() default “”;
public String table() default “”;
public ForeignKey foreignKey() default @ForeignKey(value = ConstraintMode.PROVIDER_DEFAULT);
}

Tommy_Croatia_ZGB

Tommy_Croatia_ZGB

Correct answer is A.

In the database, a relationship mapping means that one table has a reference to another table. The database term for a column that refers to a key (usually the primary key) in another table is a foreign key column. In JPA, they’re called join columns, and the @JoinColumn annotation is the primary annotation used to configure these types of columns.

The EMPLOYEE table has a foreign key column named DEPT_ID that references the DEPARTMENT
table. From the perspective of the entity relationship, DEPT_ID is the join column that associates the Employee and Department entities.

In almost every relationship, independent of source and target sides, one of the two sides will have the join column in its table. That side is called the owning side or the owner of the relationship. The side that does not have the join column is called the non-owning or inverse side. Ownership is important for mapping because the physical annotations that define the mappings to the columns in the database (for example, @JoinColumn) are always defined on the owning side of the relationship.

The main issue with an unidirectional OneToMany is that the foreign key is owned by the target object’s table, so if the target object has no knowledge of this foreign key, inserting and updating the value is difficult. In a unidirectional OneToMany the source object take ownership of the foreign key field, and is responsible for updating its value.

The target object in a unidirectional OneToMany is an independent object, so it should not rely on the foreign key in any way, i.e. the foreign key cannot be part of its primary key, nor generally have a not null constraint on it.

For example:

EMPLOYEE (table)

EMP_ID | FIRSTNAME | LASTNAME
1 | Bob | Way
2 | Sarah | Smith

PHONE (table)

ID | TYPE | AREA_CODE | P_NUMBER | OWNER_ID
1 | home | 613 | 792-0000 | 1
2 | work | 613 | 896-1234 | 1
3 | work | 416 | 123-4444 | 2

@Entity
public class Employee {
@Id
@Column(name=”EMP_ID”)
private long id;

@OneToMany
@JoinColumn(name=”OWNER_ID”, referencedColumnName=”EMP_ID”)
private List phones;

}

@Entity
public class Phone {

@Column(name=”OWNER_ID”)
private long ownerId;

}