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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:30:05+00:00 2026-05-29T20:30:05+00:00

So I asked this question yesterday, but the goal posts have changed and the

  • 0

So I asked this question yesterday, but the goal posts have changed and the question is different:

Hibernate / JPA Collection of Elements with Many to Many relationship?

I want to know if it’s possible to create entities that will model my required relationship so that Hibernate will create my schema when I fire up my application.

The relationship I want looks like this:

1 http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/417593_10150594114269218_505554217_8657377_1475865815_n.jpg

The thing is that the Join table can actually contain rows that don’t link to any Elements. The structure represents categorising of elements based on the “type” and “value” pair and are entered in to the system outside of this particular application.

What I would like to be able to do is set my Element Hibernate Entity to contain a list of Categories, via a mapping, so that I can actually see what Categories my element belongs to AND so that hibernate creates the table for me.

Here’s what I’ve got so far:mapping this in my Element Entity class like this:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
        @JoinColumn(name = "type", referencedColumnName = "type"),
        @JoinColumn(name = "value", referencedColumnName = "value") })
@Column(name = "category")
public List<ElementCategory> getCategories() {
    return categories;
}

This does most of what I want, it creates my tables as above, exactly how I want them bar one thing, there’s a Unique Constraint added in to the Element Table on the (type,value) pair. I don’t want this because multiple elements can have the same type and value pair, I need to be able to stop the Unique Constraint from begin created, but can’t figure out how with the current mapping, can I do this? Am I missing the point of a Many to Many relationship?

  • 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-29T20:30:06+00:00Added an answer on May 29, 2026 at 8:30 pm

    It actually seems quite logical that Hibernate puts a unique constraint on the type and value column.

    You say in the @ManyToMany mapping that in the jointable the joincolumns are the type and value column. So basically you say that hibernate should determine which element is coupled to the ElementCategory by the value and type property. therefore the combination ofthose 2 properties should be unique. Otherwise hibernate would not know which Element belong to what ElementType

    If you want that multiple Element entities can be coupled to multiple ElementType Entities and the combination of type and value is not always unique, then you can’t use those properties as joincolumns

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

Sidebar

Related Questions

i asked this question yesterday but it doesnt seem to have asked properly so
I already asked a question about this yesterday, but now I have another :)
I asked a question about different testing frameworks yesterday. This question can be found
I asked a similar question like this yesterday but after waiting for ever I
I asked a fairly poor question yesterday, but I still have the same general
I asked this question yesterday, but I wrote it far more complicated than I
I asked this question yesterday got a pretty good answer but can't figure out
I asked this question yesterday. But now I realize it was the wrong question.
This is a followup to a question I asked yesterday: Have you ever had
I asked this question yesterday but not sure if I made clear what I

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.