Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate OneToMany Mapping - Where Set of Entities is getting saved?
    primarykey
    data
    text
    <p>I have two entity class named <code>Customer</code> and <code>Activity</code>. </p> <p><code>Customer</code> has <code>customerId</code>,<code>customerName</code>,<code>activities</code> here <code>activities</code> holds set of <code>Activity</code> corresponds to each <code>Customer</code> mapped by <code>@OneToMany(mappedBy = "customer")</code> relationship.</p> <p>The <code>Customer</code> class has been defined as follows (I have removed other fields and some getter and setter for clarity):</p> <pre><code>@javax.persistence.Entity @Table(name = "CUSTOMER") public class Customer extends Entity { private String customerId; private String customerName; private Set&lt;Activity&gt; 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&lt;Activity&gt; getActivities() { return activities; } } </code></pre> <p><code>Activity</code> has <code>activityId</code>,<code>activityName</code>,<code>customer</code> which is mapped by <code>customerId</code> with <code>@ManyToOne</code> relation. </p> <p><code>Activity</code> class is defined like:</p> <pre><code>@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; } } </code></pre> <p>Before saving a new <code>Activity</code>, I am adding this <code>Activity</code> to the <code>activities</code> set of <code>Customer</code>.</p> <p>I want to know: </p> <ul> <li>Where the <code>activities</code> of <code>Customer</code> are getting saved ?</li> <li>How the update and delete operation of both of the entities will effect each other?</li> <li>Is it the right way to create <code>@OneToMany</code> and <code>@ManyToOne</code> relationship?</li> <li>Can I add column name on <code>activities</code>?</li> </ul> <p>I am beginner in hibernate, any pointer would be very helpful to me. </p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload