gpt4 book ai didi

java - 从数据库向导 jpa 2.1 @ManyToMany 创建实体,不需要修复列表

转载 作者:行者123 更新时间:2023-11-30 02:56:34 25 4
gpt4 key购买 nike

我有一些像这样的连接表 multimedia_feature .

向导将在多媒体类中创建一个列表属性:

...
@JoinTable(name = "multimedia_feature", joinColumns = {
@JoinColumn(name = "feature_oid", referencedColumnName = "oid")}, inverseJoinColumns = {
@JoinColumn(name = "multimedia_oid", referencedColumnName = "oid")})
@ManyToMany
private List<Feature> featureList;
...

由于我只需要多对一关系(一个功能可以有许多多媒体文件),因此我将多媒体_oid 标记为唯一。之后向导创建其他 2 个表(我认为是多余的)

@Entity
@Table(name = "multimedia_feature")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MultimediaFeature.findAll", query = "SELECT m FROM MultimediaFeature m"),
@NamedQuery(name = "MultimediaFeature.findByMultimediaOid", query = "SELECT m FROM MultimediaFeature m WHERE m.multimediaFeaturePK.multimediaOid = :multimediaOid"),
@NamedQuery(name = "MultimediaFeature.findByFeatureOid", query = "SELECT m FROM MultimediaFeature m WHERE m.multimediaFeaturePK.featureOid = :featureOid")})
public class MultimediaFeature implements Serializable {

private static final long serialVersionUID = 1L;
@EmbeddedId
protected MultimediaFeaturePK multimediaFeaturePK;
@JoinColumn(name = "multimedia_oid", referencedColumnName = "oid", insertable = false, updatable = false)
@OneToOne(optional = false)
private Multimedia multimedia;
@JoinColumn(name = "feature_oid", referencedColumnName = "oid", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Feature feature;
...
...

@Embeddable
public class MultimediaFeaturePK implements Serializable {

@Basic(optional = false)
@NotNull
@Column(name = "multimedia_oid")
private int multimediaOid;
@Basic(optional = false)
@NotNull
@Column(name = "feature_oid")
private int featureOid;
...
...

最后它在多媒体类中添加了一个属性:

....
@OneToOne(cascade = CascadeType.ALL, mappedBy = "multimedia")
private MultimediaFeature multimediaFeature;
....

由于我有很多连接类,因此我会避免创建所有这些类。我可以手动创建属性,例如:

@JoinTable(name = "multimedia_feature",
@JoinColumn(name"feature_oid", referencedColumnName = "oid")
)
@OneToOne(optional = false)
private Feature feature;

或者这会妨碍正确的持久性?

最佳答案

看起来多媒体类中的功能属性应该是@ManyToOne关系。默认情况下,创建连接表是为了映射多对多关系和单向一对多关系。如果你想避免连接类,我认为你可以通过使用 @JoinTable 来映射要素类中的多媒体属性,如下所示:

@OneToMany
@JoinTable(name = "multimedia_feature",
joinColumns = @JoinColumn(name = "feature_oid"),
inverseJoinColumns = @JoinColumn(name = "multimedia_oid") )
private List<Multimedia> multimediaList;

如果确实需要与连接表建立双向关系,映射将如下所示:

public class Feature implements Serializable {
@OneToMany(mappedBy="feature")
private List<Multimedia> multimediaList;
...
}

public class Multimedia implements Serializable {
@ManyToOne
@JoinTable(name = "multimedia_feature",
joinColumns = @JoinColumn(name = "multimedia_oid") ,
inverseJoinColumns = @JoinColumn(name = "feature_oid"))
private Feature feature;
...
}

或者,您可以通过在多媒体表中引入联接列(例如 feture_oid)来完全删除具有双向关联的联接表。这样就可以方便的在Multimedia类中映射feature属性了:

@ManyToOne
@JoinColumn(name = "feature_oid")
private Feature feature;

在要素类中将是这样的:

@OneToMany(mappedBy="feature")
private List<Multimedia> multimediaList;

关于java - 从数据库向导 jpa 2.1 @ManyToMany 创建实体,不需要修复列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091886/

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