Hibernate MapKeyManyToMany 在不存在的情况下给出复合键
我有一个使用三向连接表的 Hibernate (3.3.1) 映射:
@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>();
...
}
基础表(在 Oracle 11g 中)是:
Name Null Type
------------------------------ -------- ----------
SITECONFIGID NOT NULL NUMBER(19)
SITETYPEINSTALLATIONID NOT NULL NUMBER(19)
INSTALLATIONCONFIGID NOT NULL NUMBER(19)
关键实体过去在数据库中具有三列主键,但现在是重新定义为:
@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;
...
}
该表有一个主键“Id”和其他每个列的外键约束+索引:
Name Null Type
------------------------------ -------- ----------
SITETYPEID NOT NULL NUMBER(19)
INSTALLATIONROLEID NOT NULL NUMBER(19)
INSTALLATIONTYPEID NOT NULL NUMBER(19)
ID NOT NULL NUMBER(19)
出于某种原因,Hibernate 认为映射的键是复合的,即使它不是,并给出我这个错误:
org.hibernate.MappingException:外键(FK1A241BE195C69C8:SiteConfig_InstConfig [SiteTypeInstallationId]))必须与引用的主键(SiteTypeInstallation [SiteTypeId,InstallationRoleId])具有相同的列数
如果我删除 installationConfigurations
上的注释并使它成为暂时的,错误就会消失。
我很困惑为什么它认为 SiteTypeInstallation 根本有一个复合键,而 @Id 明确定义了一个简单的键,并且更加困惑为什么它只选择这两列。知道为什么会发生这种情况吗? JBoss (5.0 EAP) + Hibernate 是否有可能在服务器重新启动和代码重新部署过程中以某种方式记住主键的错误想法?
提前致谢, -拉尔斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论