开发者

Hibernate MapKeyManyToMany gives composite key where none exists

开发者 https://www.devze.com 2023-01-03 20:51 出处:网络
I have a Hibernate (3.3.1) mapping of a map using a three-way join table: @Entity public class SiteConfiguration extends ConfigurationSet {

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,InstallationRo开发者_高级运维leId])

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

0

精彩评论

暂无评论...
验证码 换一张
取 消