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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:44:49+00:00 2026-06-01T12:44:49+00:00

I have an entity mapped as following: @Entity @Table(name = Groups) @IdClass(value = GroupId.class)

  • 0

I have an entity mapped as following:

@Entity
@Table(name = "Groups")
@IdClass(value = GroupId.class)
public class Group implements Serializable
{
    @Id
    @Column(name = "round_id")
    private Integer roundId;

    @Id
    @Column(name = "ordinal_nbr")
    private Integer ordinalNbr = 0;

    ...
}

It has a composite key (round_id, ordinal_nbr) to indicate the order of groups in a round.

Now imagine a join table containing entities that link the ordered groups via a self reference (from Group to Group):

CREATE TABLE GroupLinks
(
  parent_round_id INTEGER NOT NULL,
  parent_ordinal_nbr SMALLINT NOT NULL,
  child_round_id INTEGER NOT NULL,
  child_ordinal_nbr SMALLINT NOT NULL,
  PRIMARY KEY (parent_round_id, parent_ordinal_nbr, child_round_id, child_ordinal_nbr),
  FOREIGN KEY (parent_round_id, parent_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr),
  FOREIGN KEY (child_round_id, child_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr)
);

I know it’s possible to map @ManyToMany + @JoinTable + @OrderColumn for the owning entity (whichever I choose):

@ManyToMany
@JoinTable(name = "GroupLinks", joinColumns = {@JoinColumn(name = "parent_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "parent_ordinal_nbr", referencedColumnName = "ordinal_nbr")}, inverseJoinColumns = {@JoinColumn(name = "child_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "child_ordinal_nbr", referencedColumnName = "ordinal_nbr")})
@OrderColumn(name = "child_ordinal_nbr")
private List<Group> children;

Question:

For the owned side, is it supported to map the @ManyToMany(mappedBy = ...) inverse relationship with an @OrderColumn, too?

@ManyToMany(mappedBy = "parents")
@OrderColumn(name = "parent_ordinal_nbr")
private List<Group> parents;

Does the JPA 2 spec define to allow or deny this?

Thanks


Update 1:

The above is essentially a graph structure. Here’s an example:

IMAGE

The GroupLinks table contains the boxed entities. When looking at the B2 Group entity only, the parents list will contain (a,1,b,2) and (a,2,b,2). As for the children list, it will contain (b,2,c,1), (b,2,c,2), and (b,2,c,3).

As you can see the entities in the lists of B2 won’t collide, so an @OrderColumn should work okay for both relationships parents and children – at least in theory. But what’s the practice here (JPA)?

Note, that simply trying it on Hibernate and/or EclipseLink doesn’t really answer the question whether the JPA 2 spec or a JPA-compatible provider should or must support this scenario.


Update 2:

Trying the above mappings on Hibernate results in the following mapping exception:

Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.kawoolutions.bbstats.model.Group.parents column: parent_ordinal_nbr
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:340)
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:363)
    at org.hibernate.mapping.Collection.validate(Collection.java:320)
    at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:89)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1291)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    ... 9 more

Removing the @OrderColumn for parent_ordinal_nbr results in the same exception for the children relationship. It looks like Hibernate doesn’t like order columns which are also used in foreign keys.


Update 3:

Tested with EclipseLink on GlassFish… this works, no mapping exceptions.

This raises two followup questions:

  1. Does the JPA disallow foreign key order columns? It doesn’t make sense to me why they shouldn’t simply work…
  2. Is this a Hibernate bug?
  • 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-01T12:44:51+00:00Added an answer on June 1, 2026 at 12:44 pm

    The javadoc or OrderColumn has the information you’re looking for:

    The OrderColumn annotation is specified on the side of the
    relationship that references the collection that is to be ordered. The
    order column is not visible as part of the state of the entity or
    embeddable class.

    The OrderBy annotation should be used for ordering that is visible as
    persistent state and maintained by the application. The OrderBy
    annotation is not used when OrderColumn is specified.

    OrderColumn is used to add an additional column to the join table, used to maintain the order of the list. The order column, as mentioned in the javadoc, is not part of the entity state. In your case, it seems you want to order the elements in the list by one of their persistent property. In that case, you must use the OrderBy annotation (or have a getter that sorts the list before returning it).

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

Sidebar

Related Questions

I have the following mapping @Entity @SequenceGenerator(name=sacpSequenceGenerator, sequenceName=SACP_SEQ) public class Sacp { private Integer
We have a DB table that is mapped into a hibernate entity. So far
I have a table which stores historical data. It's mapped to an Entity with
I have an entity with a DateTime property that is mapped to a table
I have the following tables/views Users (View) -> UserId -> Name Roles (Table) ->
I have an abstract entity base class defined like this: public abstract class SessionItem
I have the following entity classes, which are mapped from virtually identical view model
I have an entity mapped with a One-To-One as per the following code: @Entity
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A

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.