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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:42:20+00:00 2026-06-14T07:42:20+00:00

I have a class Product and a class Part , where every part can

  • 0

I have a class Product and a class Part, where every part can only belong to one one product. Each product has a list of its parts, but a part has no reference to its product.

@Entity
@Table (name= "products")
class Product {
  @Id
  @GeneratedValue
  @Column(name = "Id")
  int id;

  @Column(name = "Name")
  String name;

  @???
  List<Part> myParts;

parts:

@Entity
@Table (name= "parts")
class Part {
  @Id
  @GeneratedValue
  @Column(name = "Id")
  int id;

  @Column(name = "Name")
  String name;
}

In my database the table ‘products’ does not store information about its parts, but the ‘parts’ table keeps track of the products in a row ‘product_id’.

products:

| id | name |

parts:

| id | name | procuct_id |

I think it is quite normal to have this contrary approaches of the OO- and the ORM “world”, but I can’t find out how to persist my objects with Hibernate to this structure!

For the one-to-many annotation I only found examples where the part-id would have been stored in the product table.
For many-to-one it seems as if I needed a reference to the product in my parts objects, isn’t it?

I hope that I am wrong! 😉

Does anybody know if there is a way to map this without chaning my class or table structure?

( I would be very, very happy if you could explain it with annotations rather than with xml 🙂 )

  • 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-06-14T07:42:21+00:00Added an answer on June 14, 2026 at 7:42 am

    I think you mean a one-to-many unidirectional mapping, try something as follows:

    @OneToMany
    @JoinColumn(name="product_id")
    List<Part> myParts;
    

    You can see an example here, @OneToMany. Check out “Example 3” in that link.


    Note: Since you have the product_id as foreign key in the parts table, it is advised to also have a Product type field in the Part class.

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

Sidebar

Related Questions

I have this part of the function : $jQ('.product-collateral .product-tabs li').each(function(index){ $jQ(this).attr('id', $jQ(this).find('h3').attr('class')); if(index
I have an object that has a char property: public class Product { public
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
Simple question on best practice. Say I have: public class Product { public string
I have three domain class class Stock { Product product } and class Product
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have the following .Net class: public class Product { public int ID {get;set;}
I have the following Grails domain class: class Product { String name Float basePrice
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
Hi everyone and couchdb pros, I have a mapping setup like such: class Product(BaseModel):

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.