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

  • SEARCH
  • Home
  • 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 4578318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:28:04+00:00 2026-05-21T20:28:04+00:00

I have two classes : Expenditure and Category. An Expenditure can have multiple Categories.

  • 0

I have two classes : Expenditure and Category. An Expenditure can have multiple Categories. I wish to implement Cascade save of all Categories(children) with Expenditure(parent). But unable to achieve this.On persisting Expenditure record no Category entries are created. Please let me know what I am doing wrong. Here are my classes:

@Entity
@Table(name="EXPENDITURE")
public class Expenditure {

    private Long id;

    @OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE}, mappedBy="expenditure")
    private Set<Category> associatedCategories = new HashSet<Category>();   

    public Expenditure() {  }

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="EXPENDITURE_ID")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public void addCategory(Category category)
    {
        this.associatedCategories.add(category);
        category.setExpenditure(this);      
    }
}

@Entity
@Table(name="CATEGORY")
public class Category {

    @Id @GeneratedValue
    @Column(name="CATEGORY_ID")
    private Long id;
    private String name;

    @ManyToOne(targetEntity=Expenditure.class)
    private Expenditure expenditure;

    public Category(){}
    public Category(String name)
    {
        this.name = name;
    }
    public Category(String name,String description)
    {
        this.name = name;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }       

    public Expenditure getExpenditure() {
        return expenditure;
    }
    public void setExpenditure(Expenditure expenditure) {
        this.expenditure = expenditure;
    }
  • 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-05-21T20:28:05+00:00Added an answer on May 21, 2026 at 8:28 pm

    Try adding

    @ManyToOne(targetEntity=Expenditure.class)
    @JoinColumn(name="parentExpenditureId")   // or whatever column name exists in your database schema
    private Expenditure expenditure;
    

    And also don’t mix up the field and property access types in your entity classes.

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

Sidebar

Related Questions

I have two classes: Parent and Child with Child: belongs_to :parent and Parent has_many
I have two classes with a parent-child relationship (the Parent class has-a Child class),
I have two classes which is in different packages (folder). How can I call
I have two classes generated by LINQ2SQL both from the same table so they
I have two classes.... Parcel and FundParcel...and I'm trying to convert an IEnumerable of
I have two classes that refer to each other, but obviously the compiler complains.
I have two classes; Area and LanguageArea . I use LanguageArea to handle translated
I have two classes that I would like to merge into a composite. These
I have two classes in a PHP application, B extending A for example. Class
I have two classes one of which inherits from the other. Im trying to

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.