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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:48:22+00:00 2026-05-20T05:48:22+00:00

I have a Product class which has a 1-to-many association to itself (composite pattern).

  • 0

I have a Product class which has a 1-to-many association to itself (composite pattern). Below are the pojo, hbm, test code and log/error respectively. Can someone explain what this error is about. Why am I getting this error when setting the parent’s primary key?

Product pojo

public class Product
{
    private Long id;

    private Set<Product> children = Collections.emptySet();

    public void addChild(final Product child)
    {
        if (children.isEmpty())
        {
            children = Sets.newHashSet();
        }
        child.setParent(this);
        children.add(child);
    }
}

hbm.xml

<class name="Product" table="PRODUCT">
        <set name="children" lazy="false" table="PRODUCT">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="Product" />
        </set>
</class>

Test

public void save()
    {
        // Level 1 - mortgage LOB
        Product mortgage = new Product();
        mortgage.setCode("Mortgage");

        Product ml = new Product();
        ml.setCode("Mortgage Loan");

        Product me = new Product();
        me.setCode("Home Equity LOC");

        //Level 2
        mortgage.addChild(ml);
        mortgage.addChild(me);

        hibernateTemplate.save(mortgage);
    }

log and error

DEBUG [org.hibernate.SQL] insert into PRODUCT (ID, CODE, NAME, STARTDATE, ENDDATE, ISDECISIONABLE, ISSELECTABLE) values (null, ?, ?, ?, ?, ?, ?)
DEBUG [org.hibernate.type.StringType] binding 'Mortgage' to parameter: 1
DEBUG [org.hibernate.type.StringType] binding null to parameter: 2
DEBUG [org.hibernate.type.TimestampType] binding null to parameter: 3
DEBUG [org.hibernate.type.TimestampType] binding null to parameter: 4
DEBUG [org.hibernate.type.BooleanType] binding 'false' to parameter: 5
DEBUG [org.hibernate.type.BooleanType] binding 'false' to parameter: 6
DEBUG [org.hibernate.SQL] call identity()
DEBUG [org.hibernate.SQL] update PRODUCT set ID=? where ID=?
DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 1
ERROR [org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.Product
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
  • 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-20T05:48:23+00:00Added an answer on May 20, 2026 at 5:48 am

    A transient object is one that has not been saved. As mapped, your relationship does not use any ‘transitive persistence’, that is to say, you have not told hibernate that when you save the parent you want to save the children. I guess the error is because you have instances of the children (doesn’t matter that the children have the same class of the parent) on the parent (in the session), and hibernate is aware of that, but it doesn’t know what to do with them.

    You can either:

    1) save the children before saving the parent
    2) add the ‘cascade’ attribute to your mapping, something like cascade=”save” or cascade=”all”

    see http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-transitive

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

Sidebar

Related Questions

I have a product table that has a many-to-many relation to itself (using a
I have a product which has been traditionally shipped as an MSI file. It
I have a table called order which has many order_items , each order_items is
I have a class Products, which has the attributes name, description and price. I
Ok so I have an abstract base class called Product, a KitItem class that
I have a product, X, which we deliver to a client, C every month,
I have a model which uses acts-as-tree. For example: class CartoonCharacter < ActiveRecord::Base acts_as_tree
I have Product and Category tables in database. One product can have many categories.
I have a file upload class which returns an image resize object if the
I have a product idea that requires integration into the Microsoft Office suite. Are

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.