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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:33:27+00:00 2026-06-01T17:33:27+00:00

I have two entity classes ‘User’ and ‘Document’. Each user has an inbox and

  • 0

I have two entity classes ‘User’ and ‘Document’. Each user has an inbox and an outbox which are in fact two List and each Document may reside in multiple inbox’s and outbox’s of users. Here are my classes:

@Entity
public class User {

    @Id
    private Long id;

    @ManyToMany(mappedBy = "userinbox", cascade=CascadeType.ALL)
    private List<Document> inbox = new ArrayList<Document>();
    @ManyToMany(mappedBy = "useroutbox", cascade=CascadeType.ALL)
    private List<Document> outbox = new ArrayList<Document>();

}

@Entity
public class Document {

    @Id
    private Long id;

    @ManyToMany(cascade=CascadeType.ALL)
    private List<User> userinbox  = new ArrayList<User>();
    @ManyToMany(cascade=CascadeType.ALL)
    private List<User> useroutbox  = new ArrayList<User>();

}

When I run the programm and try to assign a document to a user’s inbox (and vice-versa), I get the following error:

Error Code: 1364

Call: INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)
    bind => [2 parameters bound]

Internal Exception: java.sql.SQLException: Field 'useroutbox_ID' doesn't have a default value

Query: DataModifyQuery(name="userinbox" sql="INSERT INTO DOCUMENT_USER (userinbox_ID, inbox_ID) VALUES (?, ?)")

The generated association table looks like this:

DOCUMENT_USER
useroutbox_ID | outbox_ID |userinbox_ID | inbox_ID

How would I assign default values for such a many-to-many relation? Would it be better to make two association tables -> one for inbox-relation, another for outbox-relation? How would I accomplish that ? Other solutions to this problem ?

Any help highly appreciated – many thanks in advance!

  • 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-01T17:33:29+00:00Added an answer on June 1, 2026 at 5:33 pm

    I think that the better option is to have two separate tables, one per relation. Because you actually have two relations between two different entities, and not one relation with four different entities.

    So, you should add a @JoinTable annotation to each of your attributes in the Document side since the User side those relations are mapped to a property. Something like the following:

    @Entity
    public class Document {
    
        @Id
        private Long id;
    
        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name = "document_inbox", joinColumns = @JoinColumn(name = "userinbox_id"),
                   inverseJoinColumns = @JoinColumn(name = "inbox_id"))
        private List<User> userinbox  = new ArrayList<User>();
        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name = "document_outbox", joinColumns = @JoinColumn(name = "useroutbox_id"),
                   inverseJoinColumns = @JoinColumn(name = "outbox_id"))
        private List<User> useroutbox  = new ArrayList<User>();
    
    }
    

    Leave the other entity as it is now. Hope this helps.

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

Sidebar

Related Questions

I have the two entity classes, User and MyCharacter. User has a list of
I have two classes, user and role, defined as: public class User : Entity
I have a two entity classes called User and Group, both reserved words that
I have defined a many-to-many relationship between my two entity classes User and Permission.
I have two classes: User : /** @Entity @Table(name=users) */ class User { /**
I have two entity classes annotated in the following way @Entity class A {
I have two Entity classes: Order and OrderItem . Order contains a navigation property
In my program I have two classes: Collector and Entity. Collector stores Entities in
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")

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.