I've a one-to-one relationship between Customer and ShippingAddress. I want to ensure that if a Customer is deleted, the ShippingAddress is also deleted, and therefore I want to store the key of customer in the shipping_address table. I only need to navigate from the customer to the address, i.e. the relationship doesn't need to be bidirectional.
According to this JPA guide, the relationship should be mapped like this:
@Entity
public class Customer{
@OneToOne
private ShippingAddress shippingAddress;
}
However, this will cause the key of shipping_address to be stored in customer. My objection to this is that it would allow someone to insert a row into shipping_address without associating it with a customer. Similarly, it would allow someone to delate a row in customer without also deleting the associated address.
Is it possible to create a unidirectional one-to-one mapping wherein
- You can navigate from the parent (customer) to the child (shipping address)
- The key of the parent is stored in the child table