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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:10:15+00:00 2026-05-23T11:10:15+00:00

Hieveryone, i have 2 Entity classes, called Category and Product which is oneToMany relationship.

  • 0

Hieveryone, i have 2 Entity classes, called Category and Product which is oneToMany relationship.

when i add a new product with a given category into database, category side is not updated with new product. Thus categoryList holds still old products.

i am using JSF2.0 with JPA 2.

Product entity:

public class Product implements Serializable {
   @ManyToOne
   private Category category;
     //getters::setters

Category entity:

public class Category implements Serializable {
   @OneToMany(mappedBy = "category")
   private List<Product> productList;

ProductController class:

    public ProductController {
        @EJB
        private ProductService productService;
        private Category category; 
        private Product product;
        //getters::setters

    //actionMethod called by JSFpage.
    public String addProduct(){

       product.setCategory(category);

       productService.add(product);
       return "productPage.xhtml";    
    }}

This way i can`t see any update on Category side.
So i have found a solution to this problem. I changed the code in ProductController as shown in below…

  public ProductController {
            @EJB
            private ProductService productService;
            @EJB
            private CategoryService categoryService;
            private Category category; 
            private Product product;
            //getters::setters

        //actionMethod called by JSFpage.
        public String addProduct(){

           product.setCategory(category);    
           productService.add(product);

           // update category with new product
            category.getProductList().add(product);
            categoryService.update(category);

           return "productPage.xhtml";    
        }}

This way category is updated BUT, i dont want CategoryService in my ProductController.
How to implement this?
i have tried with Cascade.ALL/REFRESH... but i think i couldn
t find right combination 🙂

  • 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-23T11:10:15+00:00Added an answer on May 23, 2026 at 11:10 am

    It’s the responsibility of the developer (you) to maintain the two sides of a bidirectional relationship inside a session. JPA will only take into account the side which owns the relationship to decide what has to be updated. The owning side is the side where there is no mappedBy attribute.

    So, in your example, setting the category to the product is sufficient for JPA to insert a new product in database and link it to the category (because product is the owning side). But the inverse relationship won’t be automatically updated by JPA. You must do it manually. If you discard your category object and reload it from the database, you’ll find the new product inside the category’s list of products.

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

Sidebar

Related Questions

Hi everyone i have one question i.e. i have some database table's which are
Hi everyone and couchdb pros, I have a mapping setup like such: class Product(BaseModel):
Hi everyone I am still sort of new to SQL, I have a slight
Hi everyone I have a JFrame which has three components inside. A menu A
Hi everyone and thanks for looking. I have a webform that hooks into a
Hi everyone I have an site full of html.I need to add the search
Hi everyone: Here i have created a queue from two stacks: You add to
Hi everyone I am working on a game and I have run into a
Hi everyone I have a really strange problem. I create a new subclass of
Hi everyone i have an excel file which i want to use as source

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.