我正在尝试使用外部复合键创建一个类,并且一切正常,除了 DDL 生成没有按预期创建索引。
我会用一些代码更好地解释;我试图实现的代码部分是功能区奖励:aUser
可以有很多Ribbon
s,aRibbon
可以有很多User
s(多对多)。
Ribbon
班级:
@Entity
@Table(name = "ribbon")
public class Ribbon
{
@AttributeOverrides({
@AttributeOverride(
name = "id",
column = @Column(name = "id", nullable = false, length = 4)
),
@AttributeOverride(
name = "level",
column = @Column(name = "level", nullable = false)
)
})
@EmbeddedId
RibbonId id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "ribbon")
private Set<UserHasRibbon> userHasRibbonSet;
// ...
}
RibbonId
班级:
@Embeddable
public class RibbonId implements Serializable
{
public int id;
@Enumerated(EnumType.STRING)
public UserLevel level;
@Override
public boolean equals(Object o)
{
// ...
}
@Override
public int hashCode()
{
// ...
}
}
UserHasRibbon
班级:
@Entity
@Table(name = "user_has_ribbon")
public class UserHasRibbon
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, length = 16)
private int id;
@ManyToOne(optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(optional = false)
@JoinColumns({
@JoinColumn(name="ribbon_id", referencedColumnName="id", nullable = false),
@JoinColumn(name="ribbon_level", referencedColumnName="level", nullable = false)
})
private Ribbon ribbon;
@Column(name = "earned_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date earnedDate;
// ...
}
这非常简单,一切都运行良好。唯一的问题是我注意到自动 DDL 生成没有在user_has_ribbon
表中创建索引:
ribbon_id
有索引但未链接到ribbon
.id
ribbon_level
根本没有索引user_id
是正确的,有一个链接到的索引user
。id
我知道应该为生产禁用 DDL 生成,我只是想知道为什么它的行为不符合预期。
谢谢!