Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @ManyToMany: Why columns order in primary key depends on entity class names?(Columns order in relation table primary key doesn't depends on mappedBy attribute)
@ManyToMany: Why columns order in primary key depends on entity class names? [message #1814577] Fri, 13 September 2019 13:31 Go to next message
Oleg Shchukin is currently offline Oleg ShchukinFriend
Messages: 1
Registered: September 2019
Junior Member
I use Eclipselink 2.7.4 and try to create a simple bidirectional many-to-many mapping:
@Entity
@Table(name = "Users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String username;

    @ManyToMany
    private Set<Role> roles = new HashSet<>();
}

@Entity
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String rolename;

    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>();
}

With theese entities Eclipselink generates this relation table:
CREATE TABLE Users_ROLE (roles_ID INTEGER NOT NULL, users_ID INTEGER NOT NULL, PRIMARY KEY (roles_ID, users_ID));

Note columns order in the primary key (and it seems to me this order is incorrect).

If I change the class name `User` to `AppUser` (I change only the class name, its table name remain the same - "Users"), then Eclipselink generates:
CREATE TABLE Users_ROLE (users_ID INTEGER NOT NULL, roles_ID INTEGER NOT NULL, PRIMARY KEY (users_ID, roles_ID));

What I see is the primary key columns order depends on the alphabetical name of entity classes instead of the mappedBy attribute:
- in first case `Role`, `User`;
- in second case `AppUser`, `Role`.

And also a primary key doesn't change if I move `mappedBy` attribute from `Role` to `User`.

Is it a bug or a feature?
Re: @ManyToMany: Why columns order in primary key depends on entity class names? [message #1814774 is a reply to message #1814577] Tue, 17 September 2019 20:02 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1330
Registered: July 2009
Senior Member
I'm not sure why you would consider one way correct and another incorrect, as there is no specification that details how the default relation table is supposed to be setup or the order of the keys with in it. As this is a bidirectional mapping, both sides are setting up a mapping using this table, and without other dependencies that force a certain ordering, the class getting processed first may be done alphabetically.

You can file an enhancement request to have a way to specify the ordering of the primary keys in the join table, but as DDL generation is meant as a tool for development, and if you need the order of the key defined, you are better off creating and managing DDL files and having EclipseLink run them instead.
Previous Topic:NamedStoredProcedureQuery with OracleTypes.STRUCT IN parameter doesn't pass String value to db
Next Topic: null exception when using find
Goto Forum:
  


Current Time: Mon Oct 21 12:30:31 GMT 2019

Powered by FUDForum. Page generated in 0.01751 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top