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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:09:04+00:00 2026-05-16T14:09:04+00:00

I am learning Hibernate and just read the chapter 7.2.3 Adding columns to join

  • 0

I am learning Hibernate and just read the chapter “7.2.3 Adding columns to join tables” of the “Java Persistance with Hibernate” book. My goal is to save Item, Category and CategorizedItem in one transaction.

There is a constructor there (page 305):

public CategorizedItem(String username, Category category, Item item) {
    // Set fields
    this.username = username;

    this.category = category;
    this.item = item;

    // Set identifier values
    this.id.categoryId = category.getId();
    this.id.itemId = item.getId();

    // Guarantee referential integrity
    category.getCategorizedItems().add(this);
    item.getCategorizedItems().add(this);
}

It accepts category and item objects. If I create a Category and an Item and want to connect them with this technique, they obviously have to be persisted BEFORE, as category.getId() and item.getId() return null.

Is there “a trick in the Hibernate bag” that can cascade the saving of a join table? The join table have additional columns. I want to save all three objects in the onSuccess handler in my web page controller. All three entities or none of them must be inserted.

  • 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-16T14:09:04+00:00Added an answer on May 16, 2026 at 2:09 pm

    You said

    My goal is to save Item, Category and CategorizedItem in one transaction

    Is there a trick in the Hibernate bag that can cascade the saving of a join table ?

    Yes, use MutableInt

    @Entity
    public class CategorizedItem implements Serializable {
    
        private CategorizedItemId categorizedItemId;
    
        private String userName;
    
        private Category category;
        private Item item;
    
        /**
          * required no-arg constructor
          */
        public CategorizedItem() {}
        public CategorizedItem(CategorizedItemId categorizedItemId) {
            this.categorizedItemId = categorizedItemId;
        }
        public CategorizedItem(String userName, Category category, Item item) {
            this.userName = userName;
    
            this.categorizedItemId = new CategorizedItemId(category.getIdAsMutableInt(), item.getIdAsMutableInt());
        }
    
        @EmbeddedId
        public CategorizedItemId getCategorizedItemId() {
            return this.categorizedItemId;
        }
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="CATEGORY_ID", insertable=false, updateable=false)
        public Category getCategory() {
            return this.category;
        }
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="ITEM_ID", insertable=false, updateable=false)
        public Item getItem() {
            return this.item;
        }
    
        // setter's goes here
    
        /**
          * It MUST implements Serializable
          * It MUST overrides equals and hashCode method
          * It MUST has a no-arg constructor
          *
          * Hibernate/JPA 1.0 does not support automatic generation of compound primary key
          * You SHOULD set up manually
          */
        @Embeddable
        public static class CategorizedItemId implements Serializable {
    
            private MutableInt categoryId = new MutableInt(-1);
            private MutableInt itemId = new MutableInt(-1);
    
            /**
              * required no-arg constructor
              */
            public CategorizedItemId() {}
            public CategorizedItemId(MutableInt categoryId, MutableInt itemId) {
                this.categoryId = categoryId;
                this.itemId = itemId;
            }
    
            @Column(name="CATEGORY_ID", updateable=false, nullable=false)
            public Integer getCategoryId() {
                return this.categoryId.intValue();
            }
    
            public void setCategoryId(Integer categoryId) {
                return this.categoryId.setValue(categoryId);
            }
    
            @Column(name="ITEM_ID", updateable=false, nullable=false)
            public Integer getItemId() {
                return this.itemId.intValue();
            }
    
            public void setItemId(Integer itemId) {
                return this.itemId.setValue(itemId);
            }
    
            // getter's and setter's
    
            @Override
            public boolean equals(Object o) {
                if(!(o instanceof CategorizedItemId))
                    return null;
    
                finalCategorizedItemId other = (CategorizedItemId) o;
                return new EqualsBuilder().append(getCategoryId(), other.getCategoryId())
                                          .append(getItemId(), other.getItemId())
                                          .isEquals();
            }
    
            @Override
            public int hashCode() {
                return new HashCodeBuilder().append(getCategoryId())
                                            .append(getItemId())
                                            .toHashCode();  
            }
    
        }
    
    }
    

    Here goes Category

    @Entity
    public class Category implements Serializable {
    
        public MutableInt id = new MutableInt(-1);
    
        private List<CategorizedItem> categorizedItemList = new ArrayList<CategorizedItem>();
    
        @Transient
        public MutableInt getIdAsMutableInt() {
            return this.id;
        }
    
        @Id
        @GeneratedValue
        public Integer getId() {
            return this.id.intValue();
        }
    
        public void setId(Integer id) {
            return this.id.setValue(id);
        }
    
        @OneToMany(mappedBy="category")
        @JoinColumn(name="CATEGORY_ID", insertable=false, updateable=false)
        @Cascade(CascadeType.SAVE_UPDATE)
        public List<CategorizedItem> getCategorizedItemList() {
            return categorizedItemList;
        }
    
        // setter's
    
        /**
          * Use this method when you have a saved Item
          */
        public void addCategorizedItem(CategorizedItem categorizedItem) {
            categorizedItem.setCategorizedItemId(new CategorizedItemId(getIdAsMutableInt(), categorizedItem.getItem().getIdAsMutableInt()));
            categorizedItem.setCategory(this);
    
            getCategorizedItemList().add(categorizedItem);
        }
    
    }
    

    And Item

    @Entity
    public class Item implements Serializable {
    
        public MutableInt id = new MutableInt(-1);
    
        private List<CategorizedItem> categorizedItemList = new ArrayList<CategorizedItem>();
    
        @Transient
        public MutableInt getIdAsMutableInt() {
            return this.id;
        }
    
        @Id
        @GeneratedValue
        public Integer getId() {
            return this.id.intValue();
        }
    
        public void setId(Integer id) {
            return this.id.setValue(id);
        }
    
        @OneToMany(mappedBy="item")
        @JoinColumn(name="ITEM_ID", insertable=false, updateable=false)
        @Cascade(CascadeType.SAVE_UPDATE)
        public List<CategorizedItem> getCategorizedItemList() {
            return this.categorizedItemList;
        }
    
        // setter's
    
        /**
          * Use this method when you have a saved Category
          */
        public void addCategorizedItem(CategorizedItem categorizedItem) {
            categorizedItem.setCategorizedItemId(new CategorizedItemId(getIdAsMutableInt(), categorizedItem.getCategory().getIdAsMutableInt()));
            categorizedItem.setItem(this);
    
            getCategorizedItemList().add(categorizedItem);
        }
    
    }
    

    Now because you need categoryId and itemId before saving CategorizedItem, do as follows

    Category category = new new Category();
    Item item = new Item();
    
    session.save(new Category());
    session.save(new Item());
    session.save(new CategorizedItem(userName, category, item));
    

    Notice the cascading just works when you have either a saved Category or a saved Item. Otherwise, you need to follow the approach shown above

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

Sidebar

Related Questions

No related questions found

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.