gpt4 book ai didi

java - 映射 JPA 实体

转载 作者:行者123 更新时间:2023-12-01 11:59:16 24 4
gpt4 key购买 nike

我有很多 table

programme       episode         performer       prog_ep_perf
---------- --------- --------- ------------
programmeId episodeId performerId programmeId
progTitle episodeTitle performerName episodeId
programmeId performerId

prog_ep_perf 是一个连接表(剧集表中的programmeId可能因为连接表而变得多余?)。我总结出了以下关系,我认为是正确的

programme
@OneToMany
episode --> One programme can have many episodes
@OneToMany
performers --> One programme can have many performers

episode
@OneToOne
programme --> One episode links to one programme
@OneToMany
performers --> One episode can have many performers

performer
@OneToMany
programme --> One performer can have many programmes
@OneToMany
episode --> One performer can have many episodes

这是我设置实体的方式,正确吗?

@Entity
@Table(name = "PROGRAMME")
public class Programme {

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
private Long programmeId;

private String progTitle;

@OneToMany(
targetEntity=Performer.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="PROG_EP_PERF",
joinColumns=@JoinColumn(name="PROGRAMMEID"),
inverseJoinColumns={@JoinColumn(name="PERFORMERID")}
)
private Set<Performer> performers;

@OneToMany(
targetEntity=Performer.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="PROG_EP_PERF",
joinColumns=@JoinColumn(name="PROGRAMMEID"),
inverseJoinColumns={@JoinColumn(name="EPISODEID")}
)
private Set<Episode> episodes;
}

@Entity
@Immutable
@Table(name = "EPISODE")
public class Episode {

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
private Long episodeId;

@Type(type="com.springtests.model.Programme")
@OneToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="PROGRAMMEID")
private Programme programme;

private String episodeTitle;

@OneToMany(
targetEntity=Performer.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="PROG_EP_PERF",
joinColumns=@JoinColumn(name="EPISODEID"),
inverseJoinColumns={@JoinColumn(name="PERFORMERID")}
)
private Set<Performer> performers;

}

@Entity
@Immutable
@Table(name = "PERFORMER")
public class Performer {

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
private Long performerId;

private String performerName;

@OneToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
targetEntity = Programme.class
)
@JoinTable(
name="PROG_EP_PERF",
joinColumns=@JoinColumn(name="PERFORMERID"),
inverseJoinColumns={@JoinColumn(name="PROGRAMMEID")}
)
public Set<Programme> programmes;

@OneToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
targetEntity = Programme.class
)
@JoinTable(
name="PROG_EP_PERF",
joinColumns=@JoinColumn(name="PERFORMERID"),
inverseJoinColumns={@JoinColumn(name="EPISODEID")}
)
public Set<Episode> episodes;

}

最佳答案

如果表是固定的,并且您只需要实体来表示数据库中的内容,那么看起来程序有一个 OneToMany 到剧集,并且剧集有一个 ManyToOne 返回程序,使用 Episode.programmeId 字段作为外键:

public class Programme {
..
@OneToMany(mappedby="programme")
private Set<Episode> episodes;
..

public class Episode {
..
@ManyToOne
@JoinColumn(name = "programmeId")
private Programme programme;
..

由于 Episode.programme 关系控制数据库中的外键,因此是否要实际添加 Programme.episodes 集合取决于您。如果这样做,您必须自己维护,以便在进行更改时保持双方同步。即,如果您将剧集添加到集合中,JPA 不会自动修复节目引用,也不会在您将剧集指向该剧集时将该剧集添加到节目集合中。

对于 prog_ep_perf 表,这是一个可能难以处理的三向映射。如果要为每一行填充所有 3 个外键,则无法按照您布局的方式进行映射,因为通过将其分解为多个 1:M 映射,一个外键将始终为空。

相反,最好的解决方案是将 prog_ep_perf 表映射为实体,以便您可以在应用程序中完全按照您想要或需要的方式处理它。另一种方法是使用 Map 作为具有 ManyToMany 关系的集合类型。看 http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns有关更多信息 (JPA 1.0) 和 JPA 2.0,请参阅 https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/MapKeyColumnshttp://docs.oracle.com/javaee/6/api/javax/persistence/MapKeyJoinColumn.html ,但它可能看起来像:

public class Programme {
..
@ManyToMany
@JoinTable(name="prog_ep_perf",
joinColumns=@JoinColumn(name="episodeId"),
inverseJoinColumns=@JoinColumn(name="programmeId"))
@MapKeyJoinColumn(name="performerId")
Map<Performer, Episode> prog_ep_perf;
..
}

关于java - 映射 JPA 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com