Affected tables

Report a typo

Here are Customer and Product entity classes:

Java
@Getter @Setter
@Entity
public class Customer {

    @Id
    private long id;
    private String name;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(
            name = "customer_product",
            joinColumns = @JoinColumn(name = "customer_id"),
            inverseJoinColumns = @JoinColumn(name = "product_id"))
    private Set<Product> products = new HashSet<>();
}

@Getter @Setter
@Entity
public class Product {

    @Id
    private long id;
    private int cost;

    @ManyToMany(mappedBy = "products", cascade = CascadeType.REMOVE)
    private Set<Customer> customers = new HashSet<>();
}
Kotlin
@Entity
class Customer {
    @Id
    var id: Long = 0
    var name: String = ""

    @ManyToMany(cascade = [CascadeType.PERSIST, CascadeType.MERGE])
    @JoinTable(
        name = "customer_product",
        joinColumns = [JoinColumn(name = "customer_id")],
        inverseJoinColumns = [JoinColumn(name = "product_id")]
    )
    var products: MutableSet<Product> = HashSet()
}

@Entity
class Product {
    @Id
    var id: Long = 0
    var cost = 0

    @ManyToMany(mappedBy = "products", cascade = [CascadeType.REMOVE])
    val customers: MutableSet<Customer> = HashSet()
}

The customer, product, and customer_product tables are already filled in as shown below:

customer to product many-to-many relationship via customer_product table

Depending on the presence of CascadeType.REMOVE in a class and belonging to the owning side or inverse side, different ways of deleting entities from the database will affect different tables.

In this task, you need to match each option for removing an entity, with the tables from which entities will be removed.

Note that each of the code snippets is executed inside its transaction. customer1 is a Customer object with id=1 and product101 is a Product object with id=101. These objects are retrieved from the database.

Java

For example, here is the extended code for the product101.getCustomers().remove(customer1) option:

entityManager.getTransaction().begin();

Product product101 = entityManager.find(Product.class, 101L);
Customer customer1 = entityManager.find(Customer.class, 1L);
product101.getCustomers().remove(customer1);

entityManager.getTransaction().commit();

1) entityManager.remove(customer1)

2) entityManager.remove(product101)

3) customer1.getProducts().remove(product101)

4) product101.getCustomers().remove(customer1)

A) customer_product, customer, product

B) customer_product

C) customer_product, customer

D) no affected tables

Kotlin

For example, here is the extended code for the product101.customers.remove(customer1) option:

entityManager.transaction.begin()

val product101 = entityManager.find(Product::class.java, 101L)
val customer1 = entityManager.find(Customer::class.java, 1L)
product101.customers.remove(customer1)

entityManager.transaction.commit()

1) entityManager.remove(customer1)

2) entityManager.remove(product101)

3) customer1.products.remove(product101)

4) product101.customers.remove(customer1)

A) customer_product, customer, product

B) customer_product

C) customer_product, customer

D) no affected tables

Match the items from left and right columns
1
2
3
4
B
A
C
D
___

Create a free account to access the full topic