Hibernate MapKeyManyToMany gives composite key where none exists

Posted by larsrc on Stack Overflow See other posts from Stack Overflow or by larsrc
Published on 2010-06-16T12:38:40Z Indexed on 2010/06/16 12:42 UTC
Read the original article Hit count: 281

Filed under:
|
|
|
|

I have a Hibernate (3.3.1) mapping of a map using a three-way join table:

@Entity
public class SiteConfiguration extends ConfigurationSet {
  @ManyToMany
  @MapKeyManyToMany(joinColumns=@JoinColumn(name="SiteTypeInstallationId"))
  @JoinTable(
    name="SiteConfig_InstConfig",
    joinColumns = @JoinColumn(name="SiteConfigId"),
    inverseJoinColumns = @JoinColumn(name="InstallationConfigId")
  )
  Map<SiteTypeInstallation, InstallationConfiguration>
    installationConfigurations = new HashMap<SiteTypeInstallation, InstallationConfiguration>();
...
}

The underlying table (in Oracle 11g) is:

Name                           Null     Type      
------------------------------ -------- ----------
SITECONFIGID                   NOT NULL NUMBER(19)
SITETYPEINSTALLATIONID         NOT NULL NUMBER(19)
INSTALLATIONCONFIGID           NOT NULL NUMBER(19)

The key entity used to have a three-column primary key in the database, but is now redefined as:

@Entity
public class SiteTypeInstallation implements IdResolvable {
  @Id
  @GeneratedValue(generator="SiteTypeInstallationSeq", strategy= GenerationType.SEQUENCE)
  @SequenceGenerator(name = "SiteTypeInstallationSeq", sequenceName = "SEQ_SiteTypeInstallation", allocationSize = 1)
  long id;

  @ManyToOne
  @JoinColumn(name="SiteTypeId")
  SiteType siteType;

  @ManyToOne
  @JoinColumn(name="InstalationRoleId")
  InstallationRole role;

  @ManyToOne
  @JoinColumn(name="InstallationTypeId")
  InstType type;

...
}

The table for this has a primary key 'Id' and foreign key constraints+indexes for each of the other columns:

Name                           Null     Type      
------------------------------ -------- ----------
SITETYPEID                     NOT NULL NUMBER(19)
INSTALLATIONROLEID             NOT NULL NUMBER(19)
INSTALLATIONTYPEID             NOT NULL NUMBER(19)
ID                             NOT NULL NUMBER(19)

For some reason, Hibernate thinks the key of the map is composite, even though it isn't, and gives me this error:

org.hibernate.MappingException: Foreign key (FK1A241BE195C69C8:SiteConfig_InstConfig [SiteTypeInstallationId])) must have same number of columns as the referenced primary key (SiteTypeInstallation [SiteTypeId,InstallationRoleId])

If I remove the annotations on installationConfigurations and make it transient, the error disappears.

I am very confused why it thinks SiteTypeInstallation has a composite key at all when @Id is clearly defining a simple key, and doubly confused why it picks exactly just those two columns. Any idea why this happens? Is it possible that JBoss (5.0 EAP) + Hibernate somehow remembers a mistaken idea of the primary key across server restarts and code redeployments?

Thanks in advance, -Lars

© Stack Overflow or respective owner

Related posts about Oracle

Related posts about hibernate