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 1095609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:09:10+00:00 2026-05-17T00:09:10+00:00

Recently i am started using hibernate. to insert or update i am using saveOrUpdate()

  • 0

Recently i am started using hibernate. to insert or update i am using saveOrUpdate() function.
If i update an entry i works fine. But, with new entry hibernate generate a Query. Buts nothing gets updated in the table.

My scenario is like this,

i am using Many to One & One to Many relationship between two tables[Expense, Category]
For update or insert, i am creating two objects(Expense expense, Category category) from the
client side. In the server side, i set category object with expense object.

public void upDateExpenseTable(Expens expense, Category category) {

    expense.setCategory(category);
    Session session = Main.getSession();
    try{
        System.out.println("Inside Update Try Block");
        session = Main.getSession();
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(expense);
        tr.commit();

    }
    finally{
        session.close();
    }
}

Object structure is like, catogery.catId, category.catName &
expense.expnsId, expense.expnsName, expense.amount, expense.status, expense.userName.

But there is another one column in Expense Table cat_id. Its is through mapping annotation. But i dont have any property for that in Expense entity.
When inserting new data, i am not giving any Id.

Any suggestions!!!

public class Expens implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int expnsId;
private int amount;
private String date;
private String status;
private String userName;

    @ManyToOne
@JoinColumn(name = "cat_id") 
private Category category;
    //Setter Getter

}

Category Classs

public class Category{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;

@OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Expens> expenses;
}//Setters Getters ommited.
  • 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-17T00:09:10+00:00Added an answer on May 17, 2026 at 12:09 am

    I’m not sure I understood everything but I can at least tell you that you’re not building your bi-directional association correctly, you need to set “both sides of the link” when building your object graph:

    ...
    expense.setCategory(category);
    category.getExpenses().add(expense);
    ...
    session.saveOrUpdate(category);
    

    And this is usually done in “link management” methods, like this (in Category):

    @Entity
    public class Category {
    
        @Id @GeneratedValue(strategy=GenerationType.AUTO)
        private int catId;
        private String catName;
    
        @OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
        private List<Expens> expenses = new ArrayList<Expens>();
    
        public void addToExpenses(Expense expense) {
            expenses.add(expense);
            expense.setCategory(this);
        }    
    
        protected List getExpenses() { // protected to prevent direct access from outside the hierarchy
            return expenses;
        }
    
        protected void setExpenses(List expenses) { // protected to prevent direct access
            this.expenses = expenses;
        }
    
        //...
    }
    

    And the code becomes:

    category.addToExpenses(expense);
    session.saveOrUpdate(category);
    

    Funnily enough (or not), I’ve written about this three times today.

    Resource

    • Hibernate Core Documentation
      • 1.2.6. Working bi-directional links
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started using Hibernate recently and I'm still a newbie, however, the error I'm
I recently started using imagemagick with php and I'm relatively new with both of
I've recently started using AutoComplPop , but I find the popup annoying when I'm
I've just recently started using VS2008 for a new tools project, and have been
I started using Hibernate only recently. While I was playing with it, I experienced
I have recently started using SQLALCHEMY to query a my-sql database. I want to
I recently started using a new server for a client and am having problems
I am good in web development but recently started using PHP and developed few
I recently started using Git when creating a new project - until then I
I've recently started using Hibernate and am trying to get my head around all

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.