Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:49:24+00:00 2026-06-18T07:49:24+00:00

I have two entity class named Customer and Activity . Customer has customerId ,

  • 0

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 activities of Customer are getting saved ?
  • How the update and delete operation of both of the entities will effect each other?
  • Is it the right way to create @OneToMany and @ManyToOne relationship?
  • Can I add column name on activities?

I am beginner in hibernate, any pointer would be very helpful to me.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:49:25+00:00Added an answer on June 18, 2026 at 7:49 am

    – 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_id column, or they are loaded later (lazy-loading) using a separate query that filters the customer_id column.

    – 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_id field 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entity classes annotated in the following way @Entity class A {
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have two classes, user and role, defined as: public class User : Entity
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")
I have two classes: User and Video : @Entity class User{ @Id int id;
iam new in hibernate, i have a entity class named student, it have 5
I have two entities @Entity public class Tabulka{ @OneToMany(mappedBy = tabulka) private List<VysledkyHraca> vysledkyHraca;
I have two Hibernate entities named Alpha and Beta, and Alpha has a *-to-one
I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.