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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:05:39+00:00 2026-06-11T07:05:39+00:00

I’m having a problem creating a parent and child object and persisting them in

  • 0

I’m having a problem creating a parent and child object and persisting them in one go to the database using JPA and hibernate. The parent class looking like this:

@Entity
@Table(name = "PUser")
public final class User {

    @Id
    @Column(name = "ID", unique = true, nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

}

The child object uses a compound key, one of fields being the ID of the parent:

@Entity
@Table(name = "PAttribute")
public final class Attribute {

   @EmbeddedId
    @AttributeOverrides({
                    @AttributeOverride(name = "domain", column = @Column(name = "domain", nullable = false, length = 128)),
                    @AttributeOverride(name = "name", column = @Column(name = "name", nullable = false, length = 128)),
                    @AttributeOverride(name = "userid", column = @Column(name = "userid", nullable = false)) })
    private AttributePk pk;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userid", nullable = false, insertable = false, updatable = false)
    private User user;

}

The composite key class being:

    @Embeddable
    public class AttributePk implements java.io.Serializable {

            private static final long serialVersionUID = -7003721226789641149L;

            @Column(nullable = false, length = 128)
            private String domain;

            @Column(nullable = false, length = 128)
            private String name;

            @Column(nullable = false)
            private long userid;

    }

Now when I create a new User and add an Attachment, and then attempt to save the object graph

    User user = new User("joe1.bloggs@ft.com", "xyz");
    user.setScreenName("joe1bloggs");
    user.addAttribute("domain", "name", 212);

    target.saveAndFlush(user);  

I get the exception

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`fta_portal_user/PAttribute`, CONSTRAINT `userId` FOREIGN KEY (`userid`) REFERENCES `PUser` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

Obviously the Attribute child object doesn’t have the ID of the user object set.

I know I can do the following, which works, but it seems odd that I can’t just create an object hierarchy and Hibernate work out how to set the IDs accordingly. I suspect that there’s no way round this as saveAndFlush() returns a new instance rather than just updating the input parameter version.

    User user = new User("joe1.bloggs@ft.com", "xyz");
    user.setScreenName("joe1bloggs");

    // target is an JpaRepository intergafe
    user = target.saveAndFlush(user);

    user.addAttribute("domain", "name", 212);

    target.saveAndFlush(user);

Anyone have any ideas or is it just a case of living with it?

Thanks

Nick

EDIT: You can get this to work by adding the @MapsId annotation to the child class (thanks to axtavt’s reply). i.e.

    @MapsId("userId")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId", nullable = false, insertable = false, updatable = false)
    private User user;
  • 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-11T07:05:41+00:00Added an answer on June 11, 2026 at 7:05 am

    Have you tried to map it as follows:

    @ManyToOne(fetch = FetchType.LAZY)     
    @MapsId("userid")
    private User user; 
    

    As far as I understand in this case userid field of AttributePk should be populated with the id of user automatically.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.