我有REGION表:
REG {
id number,
dateCreation Date
}
我有一张LOCALE表:
LOCALE {
locale String
}
我有一个REGION_DESCRIPTION表:
REGION_DESCRIPTION {
locale String,
regId number,
description
}
REGION_DESCRIPTION有一个复合键:locale是指向LOCALE表的外键。并且regId指向REGION表。
我想将此映射到我的Region java类:
private List<RegionDescription> descriptions;
我在JPA尝试了不同的方法。但我无法使映射工作。有什么建议?
答案 0 :(得分:2)
您有一个连接表(REGION_DESCRIPTION)通过携带自己的属性成为实体的情况。我建议如下:
将 RegionDescription 定义为 完整的实体与复合 主键类,请参阅example;
在 RegionDescription 中定义2个多对一关系: RegionDescription 到 Region 和 RegionDescription 到区域设置。
或者另外定义 Region 中的一对多关系 映射 RegionDescription 的方式 你在上面指定的。
答案 1 :(得分:1)
这应该满足您的需求:
@Entity
public class Region
{
@Id
private Long id;
@Temporal(TemporalType.DATE)
private Date dateCreation;
@OneToMany(mappedBy = "region")
@JoinColumn(name = "id", referencedColumnName = "region_id")
private List<RegionDescription> descriptions;
}
@Entity
public class Locale
{
@Id
private String name;
}
@Entity
public class RegionDescription
{
@EmbeddedId
private RegionDescriptionPK key;
@MapsId(value = "regionId")
@ManyToOne
@JoinColumns({
@JoinColumn(name = "region_id", referencedColumnName = "id")
})
private Region region;
@MapsId(value = "localeName")
@ManyToOne
@JoinColumns({
@JoinColumn(name = "locale", referencedColumnName = "name")
})
private Locale locale;
}
@Embeddable
public class RegionDescriptionPK
{
@Column(name = "id")
private Long regionId;
@Column(name = "locale")
private String localeName;
}
作为旁注,我看到这个问题很老了。但它并没有以某种方式关闭。 : - )
答案 2 :(得分:0)
这应该有效:
@Entity
public class Region
{
@Id
private Long id;
@Temporal(TemporalType.DATE)
private Date dateCreation;
@OneToMany(mappedBy = "regionDescriptionPK.region")
private List<RegionDescription> descriptions;
}
@Entity
public class RegionDescription
{
@EmbeddedId
private RegionDescriptionPK regionDescriptionPK;
}
@Embeddable
public class RegionDescriptionPK
{
@ManyToOne
@JoinColumn(name = "Region_ID")
private Region region;
@ManyToOne
@JoinColumn(name = "locale")
private Locale locale;
}
@Entity
public class Locale
{
@Id
private String name;
}