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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:38+00:00 2026-06-12T09:14:38+00:00

I have a user, who has a many books: public class User { @OneToMany

  • 0

I have a user, who has a many books:

public class User {

    @OneToMany
    @Cascade( { org.hibernate.annotations.CascadeType.ALL })
    @JoinTable(name="user_book")
    private Set<Book> books;

}

this will create in database a join table:

|user_id|book_id|
-----------------
|1      |2      |
|1      |3      |
|2      |1      |  

Problem is that unique constraint is only on book_id column.
That means 2 user can’t have the same book.
If I would like add to user(id:1) a book(id:1), then I got: BatchUpdateException: Duplicate entry

Is this normal ? or this is bug?
If not a bug, how i can configure hibernate to create unique constraint on pair (user_id,book_id) not only on book_id in join table.

Hibernate: 3.6.4.Final

MySQL: 5.0.21

  • 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-12T09:14:39+00:00Added an answer on June 12, 2026 at 9:14 am

    Your association is a One To Many. This means that One user can have Many books. If you want to be able to have a book shared by several users, make it a ManyToMany association: a user would have several books, and a book would have several users.

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

Sidebar

Related Questions

I have a list of the user's images displayed, (who has already allowed access
I have three models: User Award Trophy The associations are: User has many awards
I have an application that has many users who can login and perform insertion,
I have a User model which has many projects and a Project model which
I have a data object (lets call it employee) who has many get/set methods
I have a user who owns many phones I have a a phone which
I have a user who has to use Microsoft Word 2007 to create and
Say I have Author and Book domain classes (one Author has many Books ).
I'm working on a paperclip upload system where I have a User who has
I have a user who randomly gets a Script Error box (shown below) which

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.