I have two entity class named Customer and Activity.
Customer has customerId,customerName,activities here activities holds set of Activity corresponds to each Customer mapped by @OneToMany(mappedBy = "customer") relationship.
The Customer class has been defined as follows (I have removed other fields and some getter and setter for clarity):
@javax.persistence.Entity
@Table(name = "CUSTOMER")
public class Customer extends Entity {
private String customerId;
private String customerName;
private Set<Activity> activities;
@NaturalId
@Column(name = "CUSTOMER_ID", nullable = false)
public String getCustomerId() {
return customerId;
}
@Column(name = "CUSTOMER_NAME", nullable = false)
public String getCustomerName() {
return customerName;
}
@OneToMany(mappedBy = "customer", fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
public Set<Activity> getActivities() {
return activities;
}
}
Activity has activityId,activityName,customer which is mapped by customerId with @ManyToOne relation.
Activity class is defined like:
@javax.persistence.Entity
@Table(name = "ACTIVITY")
public class Activity extends Entity {
private String activityId;
private String activityName;
private Customer customer;
@NaturalId
@Column(name = "ACTIVITY_ID", nullable = false)
public String getActivityId() {
return activityId;
}
@Column(name = "ACTIVITY_NAME", nullable = false)
public String getActivityName() {
return activityName;
}
@ManyToOne
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
public Customer getCustomer() {
return customer;
}
}
Before saving a new Activity, I am adding this Activity to the activities set of Customer.
I want to know:
- Where the
activitiesofCustomerare getting saved ? - How the update and delete operation of both of the entities will effect each other?
- Is it the right way to create
@OneToManyand@ManyToOnerelationship? - Can I add column name on
activities?
I am beginner in hibernate, any pointer would be very helpful to me.
Thanks in advance.
– Where the activities of Customer are getting saved ?
They are not getting saved as “activities of customer” anywhere in the DB.When the activities of a customer are needed, they are loaded together with the customer using a join query on the
customer_idcolumn, or they are loaded later (lazy-loading) using a separate query that filters thecustomer_idcolumn.– How the update and delete operation of both of the entities will effect each other?
A delete of an activity will not affect the customer, but next time its activities are loaded, the deleted activity will not be part of the collection anymore.
How the delete of a customer activities can be defined using the cascade attribute on the
@ManyToOne, for example :@OneToMany(mappedBy = "customer", cascade = CascadeType.REMOVE)Basically, the desired behavior depends on if an activity can exist on its own without a customer : if yes, then the customer can be deleted, and the
customer_idfield can be set to null on the activity table. Otherwise the activities belonging to a customer will be ‘cascade-deleted’. Or a customer just can0t be deleted before one has removed all its activities first.– Is it the right way to create @OneToMany and @ManyToOne relationship?
Yes, it is correct.
– Can I add column name on activities?
No, it wouldn’t make any sense. A column could only contain one ID, and activities is a collection.
I hope this helps.