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

  • Home
  • SEARCH
  • 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 979983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:18:24+00:00 2026-05-16T04:18:24+00:00

I’m having a problem modelling the following problem in JPA. I have a JPA

  • 0

I’m having a problem modelling the following problem in JPA. I have a JPA Entity class ‘User’, like so:
(accessors/mutators/extraneous fields/extraneous JPA configuration omitted for brevity)


    @Entity
    class User {
        @Id
        @Generated Value
        long id;
        @OneToMany
        Report contributorReports; // All the reports where this user is contributor
        @OneToMany ownerReports; // All the reports where this user is owner
        String username;
    }

and a JPA Entity class ‘Report’


@Entity
class Report {
    @Id
    @GeneratedValue
    long id;
    @OneToOne
    User contributor;
    @OneToOne
    User owner;
    SomeData data;
}

I would like to model a relationship such that:

  • A Report must contain both a contributor and an owner
  • I can access all of the Reports a User has been a ‘contributor’ for via the User Entity
  • I can access all of the Reports a User has been a ‘owner’ for via the User Entity

I imagined I would end up with a mapping table that looked vaguely like this:


CREATE TABLE user_report {
    BIGINT reportId,
    BIGINT contributorId,
    BIGINT ownerId,
}

I attempted to solve the problem like:


    @OneToOne
    @JoinTable(name = "user_report",
            joinColumns = {
                    @JoinColumn(name = "reportOwner_ID", referencedColumnName = "ID")}
            )
    private User owner;
    @OneToOne
    @JoinTable(name = "user_report",
            joinColumns = {
                @JoinColumn(name = "reportContributor_ID", referencedColumnName = "ID")}
            )
    private User contributor;

This generates a table like:

CREATE TABLE user_report (
    BIGINT ownerReport_ID, // Report ID
    BIGINT reportOwner_ID, // UserID owner
    BIGINT contributorReport_ID, // Report ID
    BIGINT reportContributor_ID // UserID contributor
)

So when JPA attempts to map to this table, it maps each field separately and fails because only half of the row is being committed, throwing this exception:


Caused by: java.sql.BatchUpdateException: Field 'ownerReport_ID' doesn't have a default value

I was hoping to get some direction on how to best model the relationship I envision. (Or maybe a better way to envision the relationship) If additional information is needed, I’ll be glad to provide it.

Kind Regards
Matt

  • 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-16T04:18:25+00:00Added an answer on May 16, 2026 at 4:18 am

    Based on your requirements I believe you accomplish this with 2 1:M from User to Report with a matched M:1 back for each.

    @Entity
    class User {
        @Id
        @Generated Value
        long id;
    
        // All the reports where this user is contributor
        @OneToMany(mappedBy="contributor")
        List<Report> contributorReports; 
    
        // All the reports where this user is owner
        @OneToMany(mappedBy="owner")
        List<Report> ownerReports; 
    
        String username;
    }
    

    Then your Report class would look like:

    @Entity
    class Report {
        @Id
        @GeneratedValue
        long id;
    
        @ManyToOne
        User contributor;
    
        @ManyToOne
        User owner;
    
        SomeData data;
    }
    

    This situation is also possible with a join table but not required based on your requirements as I understand them.

    Doug

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

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.