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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:46:54+00:00 2026-05-15T03:46:54+00:00

I’m stuck with a database which is using multi-field primary keys. I have a

  • 0

I’m stuck with a database which is using multi-field primary keys. I have a situation where I have a master and details table, where the details table’s primary key contains fields which are also the foreign key’s the the master table. Like this:

Master primary key fields:
    master_pk_1

Details primary key fields:
    master_pk_1
    details_pk_2
    details_pk_3

In the Master class we define the hibernate/JPA annotations like this:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator")
@Column(name = "master_pk_1")
private long masterPk1;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "master_pk_1", referencedColumnName = "master_pk_1")
private List<Details> details = new ArrayList<Details>();

And in the details class I have defined the id and back reference like this:

@EmbeddedId
@AttributeOverrides( { 
        @AttributeOverride( name = "masterPk1", column = @Column(name = "master_pk_1")),
        @AttributeOverride(name = "detailsPk2", column = @Column(name = "details_pk_2")),
        @AttributeOverride(name = "detailsPk2", column = @Column(name = "details_pk_2")) })
private DetailsPrimaryKey detailsPrimaryKey = new DetailsPrimaryKey();

@ManyToOne
@JoinColumn(name = "master_pk_1", referencedColumnName = "master_pk_1", insertable=false)
private Master master;

The goal of all of this was that I could create a new master, add some details to it, and when saved, JPA/Hibernate would generate the new id for master in the masterPk1 field, and automatically pass it down to the details records, storing it in the matching masterPk1 field in the DetailsPrimaryKey class. At least that’s what the documentation I’ve been looking at implies.

What actually happens is that hibernate appears to corectly create and update the records in the database, but not pass the key to the details classes in memory. Instead I have to manually set it myself.

I also found that without the insertable=true added to the back reference to master, that hibernate would create sql that had the master_pk_1 field listed twice in the insert statement, resulting in the database throwing an exception.

My question is simply is this arrangement of annotations correct? or is there a better way of doing it?

  • 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-15T03:46:55+00:00Added an answer on May 15, 2026 at 3:46 am

    Could you add the DetailsPrimaryKey mapping also ?

    What seems strange in your mapping is that you are mapping twice the ‘master_pk_1’ column in the details tale. By adding the insertable=false option, now inserting works, but i guess updates still not works (in fact when you map columns more than once in an entity you must annotate all but one of them with insertable=false, updatable=false)

    To correct that, i think you should move the @ManyToOne Master to the detail id (removing the insertable=false option).

    You should also add the option mappedBy=”detailsPrimaryKey.master” to the @OneToMany annotation, and maybe also a cascade if you wish the childs to be automatically saved.

    (look at http://beavercreekconsulting.com/blog/2008/10/hibernate-annotations-for-a-one-to-many-mapping/ you will see a code solving something quite similar to your problem)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I used javascript for loading a picture on my website depending on which small
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.