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

The Archive Base Latest Questions

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

I am facing a problem with parent child type relations. Hibernate docs say to

  • 0

I am facing a problem with parent child type relations.
Hibernate docs say to add a “many-to-one” relation in child class to get value of foreign key from parent. But to make this relation work I have to add Invoice property in child class that introduce a circular inclusion of parent into child and breaks my serializer. Could somebody point where am I doing mistake?

Here is my code:

Invoice.java

public class Invoice implements Serializable {
  private Long id;
  private Date invDate;
  private String customer;
  private Set<InvoiceItem> items;
  ... getters/setters ...
}

InvoiceItem.java

public class InvoiceItem implements Serializable {
  private Long itemId;
  private long productId;
  private int quantity;
  private double price;
  private Invoice invoice; //???????
  ... getters/setters ...
}

Invoice.hbm.xml

<class name="Invoice" table="Invoices">
  <id name="id" column="ID" type="long">
    <generator class="native" />
  </id>
  <property name="invDate" type="timestamp" />
  <property name="customer" type="string" />

  <set name="items" inverse="true" cascade="all-delete-orphan">
    <key column="invoiceId" />
    <one-to-many class="InvoiceItem" />
  </set>
</class>

InvoiceItem.hbm.xml

<class name="InvoiceItem" table="InvoiceItems">
  <id name="itemId" type="long" column="id">
    <generator class="native" />
  </id>

  <property name="productId" type="long" />
  <property name="quantity" type="int" />
  <property name="price" type="double" />

<many-to-one name="invoiceId" class="Invoice" not-null="true"/> <!--????????-->
</class>
  • 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-15T18:17:24+00:00Added an answer on May 15, 2026 at 6:17 pm

    You don’t have to have a reference to Invoice in InvoiceItem if you remove the inverse=”true” attribute.
    Hibernate will then create a separate mapping table rather than use a foreign key in the InvoiceItem table.

    Remove the inverse attribute on the InvoiceItem set, and also remove the Invoice property from InvoiceItem, and the corresponding many-to-one in the mapping and you should get what you want.

    Alternatively, you could mark the Invoice reference in InvoiceItem as transient, and handle populating the value during deserialization: iterate over the Set of Items in Invoice, and set the invoice property on each item to the owning invoice.

    • 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.