[英]Embeddable class contains composite key generates too many columns?
我有兩個類(UmowaEntity 和 MpkEntity)。 我將@ManyToMany 關聯替換為兩個雙向@OneToMany 關聯,因為我需要額外的列(“aktywny”)。 MpkEntity 包含復合鍵(CompositeKey)。
Mpk實體:
@Entity
@Table(name = "mpk")
public class MpkEntity implements Serializable {
private CompositeKey compositeKey;
private String nazwaSkrocona;
private Boolean aktywny = Boolean.TRUE;
private Set<UmowaMpkEntity> umowy = new HashSet<>();
public MpkEntity() {
}
@EmbeddedId
public CompositeKey getCompositeKey() {
return compositeKey;
}
public void setCompositeKey(CompositeKey compositeKey) {
this.compositeKey = compositeKey;
}
@JsonView(ViewAlways.class)
@Transient
public Long getId() {
return compositeKey.getId();
}
@Column(name = "nazwa_skrocona", nullable = false)
@JsonView(ViewAlways.class)
public String getNazwaSkrocona() {
return nazwaSkrocona;
}
public void setNazwaSkrocona(String nazwaSkrocona) {
this.nazwaSkrocona = nazwaSkrocona;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "mpk", cascade = CascadeType.ALL)
public Set<UmowaMpkEntity> getUmowy() {
return umowy;
}
public void setUmowy(Set<UmowaMpkEntity> umowy) {
this.umowy = umowy;
}
public Boolean getAktywny() {
return aktywny;
}
public void setAktywny(Boolean aktywny) {
this.aktywny = aktywny;
}
}
Umowa實體:
@Entity
@Table(name = "umowa")
public class UmowaEntity implements Serializable {
private Long id;
private String numer;
private String przedmiot;
private Set<UmowaMpkEntity> mpki = new HashSet<>();
public UmowaEntity() {
}
@Id
@JsonView(ViewAlways.class)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "numer", unique = true)
@NotNull
@JsonView(ViewAlways.class)
public String getNumer() {
return numer;
}
public void setNumer(String numer) {
this.numer = numer;
}
@Column(name = "przedmiot")
@NotNull
@JsonView(ViewAlways.class)
public String getPrzedmiot() {
return przedmiot;
}
public void setPrzedmiot(String przedmiot) {
this.przedmiot = przedmiot;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "umowa", cascade = CascadeType.ALL)
@JsonView(UmowaView.MainView.class)
public Set<UmowaMpkEntity> getMpki() {
return mpki;
}
public void setMpki(Set<UmowaMpkEntity> mpki) {
this.mpki = mpki;
}
}
復合鍵:
@Embeddable
public class CompositeKey implements Serializable {
private static final long serialVersionUID = 1338879188344812866L;
private Long id;
private FirmaEntity firma;
public CompositeKey() {
}
public CompositeKey(Long id, FirmaEntity firma) {
this.id = id;
this.firma = firma;
}
@JsonView(ViewAlways.class)
@Column(insertable = true, updatable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonView(ViewAlways.class)
@ManyToOne
@JoinColumn(name = "firma_id", nullable = false, insertable = true, updatable = false)
public FirmaEntity getFirma() {
return firma;
}
public void setFirma(FirmaEntity firma) {
this.firma = firma;
}
@Override
public int hashCode() {
int hash = 5;
hash = 67 * hash + Objects.hashCode(this.id);
hash = 67 * hash + Objects.hashCode(this.firma);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof CompositeKey)) {
return false;
}
final CompositeKey other = (CompositeKey) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
if (!Objects.equals(this.firma, other.firma)) {
return false;
}
return true;
}
}
公司實體:
@Entity
@Table(name = "firma")
public class FirmaEntity extends BaseEntity {
private static final long serialVersionUID = -4105296181552840561L;
private Long id;
private String nazwa;
public FirmaEntity() {
}
public FirmaEntity(Long firmaId) {
this.id = firmaId;
}
@Id
@JsonView(ViewAlways.class)
@Cacheable("firmy")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonView(ViewAlways.class)
@Column(nullable = false)
public String getNazwa() {
return nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
@Override
public String toString() {
return "FirmaEntity{" + "id=" + id + ", nazwa=" + nazwa + '}';
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof FirmaEntity)) {
return false;
}
final FirmaEntity other = (FirmaEntity) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}
我嘗試創建包含復合鍵的@Embeddable class。 我懷疑 hibernate 會創建 4 列(aktywny,umowa_id(pk),mpk_id(pk),firma_id(pk)),但我收到了 6 列(id(pk),aktywny,umowa_id(pk),mpk_id,firma_id(pk) , mpk_firma_id)。 Now when I try to insert UmowaMpkEntity I got UmowaMpkEntity ERROR: null value in column "id" violates not-null constraint: Failing row contains (null, t, 131, null, null, null) due to column "id".
UmowaMpk實體:
@Entity
@Table(name = "umowa_mpk")
public class UmowaMpkEntity implements Serializable {
private UmowaMpkId pk = new UmowaMpkId();
private Boolean aktywny = Boolean.TRUE;
private UmowaEntity umowa;
private MpkEntity mpk;
private FirmaEntity firma;
public UmowaMpkEntity() {
}
@EmbeddedId
public UmowaMpkId getPk() {
return pk;
}
public void setPk(UmowaMpkId pk) {
this.pk = pk;
}
@JsonView(UmowaView.MainView.class)
public Boolean getAktywny() {
return aktywny;
}
public void setAktywny(Boolean aktywny) {
this.aktywny = aktywny;
}
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("umowaId")
public UmowaEntity getUmowa() {
return umowa;
}
public void setUmowa(UmowaEntity umowa) {
this.umowa = umowa;
}
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("compositeKey.id")
@JsonView(UmowaView.MainView.class)
public MpkEntity getMpk() {
return mpk;
}
public void setMpk(MpkEntity mpk) {
this.mpk = mpk;
}
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("compositeKey.firma.id")
@JsonView(UmowaView.MainView.class)
public FirmaEntity getFirma() {
return firma;
}
public void setFirma(FirmaEntity firma) {
this.firma = firma;
}
@Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + Objects.hashCode(this.umowa.getId());
hash = 89 * hash + Objects.hashCode(this.mpk.getCompositeKey().getId());
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UmowaMpkEntity other = (UmowaMpkEntity) obj;
if (!Objects.equals(this.umowa.getId(), other.umowa.getId())) {
return false;
}
if (!Objects.equals(this.mpk.getCompositeKey().getId(), other.mpk.getCompositeKey().getId())) {
return false;
}
return true;
}
}
UmowaMpkId:
@Embeddable
public class UmowaMpkId implements Serializable {
private Long umowaId;
private CompositeKey compositeKey;
public UmowaMpkId() {
}
@Column(name = "umowa_id")
public Long getUmowaId() {
return umowaId;
}
public void setUmowaId(Long umowaId) {
this.umowaId = umowaId;
}
public CompositeKey getCompositeKey() {
return compositeKey;
}
public void setCompositeKey(CompositeKey compositeKey) {
this.compositeKey = compositeKey;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + Objects.hashCode(this.umowaId);
hash = 41 * hash + Objects.hashCode(this.compositeKey);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UmowaMpkId other = (UmowaMpkId) obj;
if (!Objects.equals(this.umowaId, other.umowaId)) {
return false;
}
if (!Objects.equals(this.compositeKey, other.compositeKey)) {
return false;
}
return true;
}
}
我猜 UnowaMpkEntity 的 @MapsId 有問題。 它應該是這樣的
@Entity
@Table(name = "umowa_mpk")
public class UmowaMpkEntity implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("compositeKey") // without id, as MpkEntity has it as its PK
@JsonView(UmowaView.MainView.class)
public MpkEntity getMpk() {
return mpk;
}
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("compositeKey.firma") // without id, as compositeKey.firma is already referenced to it
@JsonView(UmowaView.MainView.class)
public FirmaEntity getFirma() {
return firma;
}
}
PS 考慮為您的課程使用英文名稱
PPS 為簡潔起見考慮使用Lombok
我建議您不要在復合 ID 中使用關聯,而是僅在實體中使用 map 它們。 嘗試這個:
@Entity
@Table(name = "mpk")
public class MpkEntity implements Serializable {
private CompositeKey compositeKey;
private FirmaEntity firma;
private String nazwaSkrocona;
private Boolean aktywny = Boolean.TRUE;
private Set<UmowaMpkEntity> umowy = new HashSet<>();
public MpkEntity() {
}
@EmbeddedId
public CompositeKey getCompositeKey() {
return compositeKey;
}
public void setCompositeKey(CompositeKey compositeKey) {
this.compositeKey = compositeKey;
}
@JsonView(ViewAlways.class)
@ManyToOne
@JoinColumn(name = "firma_id", nullable = false, insertable = false, updatable = false)
public FirmaEntity getFirma() {
return firma;
}
public void setFirma(FirmaEntity firma) {
this.firma = firma;
}
@JsonView(ViewAlways.class)
@Transient
public Long getId() {
return compositeKey.getId();
}
@Column(name = "nazwa_skrocona", nullable = false)
@JsonView(ViewAlways.class)
public String getNazwaSkrocona() {
return nazwaSkrocona;
}
public void setNazwaSkrocona(String nazwaSkrocona) {
this.nazwaSkrocona = nazwaSkrocona;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "mpk", cascade = CascadeType.ALL)
public Set<UmowaMpkEntity> getUmowy() {
return umowy;
}
public void setUmowy(Set<UmowaMpkEntity> umowy) {
this.umowy = umowy;
}
public Boolean getAktywny() {
return aktywny;
}
public void setAktywny(Boolean aktywny) {
this.aktywny = aktywny;
}
}
@Embeddable
public class CompositeKey implements Serializable {
private static final long serialVersionUID = 1338879188344812866L;
private Long id;
private Long firmaId;
public CompositeKey() {
}
public CompositeKey(Long id, FirmaEntity firma) {
this.id = id;
this.firmaId = firma.getId();
}
@JsonView(ViewAlways.class)
@Column(insertable = true, updatable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonView(ViewAlways.class)
@ManyToOne
@Column(name = "firma_id", nullable = false, insertable = true, updatable = false)
public Long getFirmaId() {
return firmaId;
}
public void setFirmaId(Long firmaId) {
this.firmaId = firmaId;
}
@Override
public int hashCode() {
int hash = 5;
hash = 67 * hash + Objects.hashCode(this.id);
hash = 67 * hash + Objects.hashCode(this.firmaId);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof CompositeKey)) {
return false;
}
final CompositeKey other = (CompositeKey) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
if (!Objects.equals(this.firmaId, other.firmaId)) {
return false;
}
return true;
}
}
@Entity
@Table(name = "umowa")
public class UmowaEntity implements Serializable {
private Long id;
private String numer;
private String przedmiot;
private Set<UmowaMpkEntity> mpki = new HashSet<>();
public UmowaEntity() {
}
@Id
@JsonView(ViewAlways.class)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "numer", unique = true)
@NotNull
@JsonView(ViewAlways.class)
public String getNumer() {
return numer;
}
public void setNumer(String numer) {
this.numer = numer;
}
@Column(name = "przedmiot")
@NotNull
@JsonView(ViewAlways.class)
public String getPrzedmiot() {
return przedmiot;
}
public void setPrzedmiot(String przedmiot) {
this.przedmiot = przedmiot;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "umowa", cascade = CascadeType.ALL)
@JsonView(UmowaView.MainView.class)
public Set<UmowaMpkEntity> getMpki() {
return mpki;
}
public void setMpki(Set<UmowaMpkEntity> mpki) {
this.mpki = mpki;
}
}
@Entity
@Table(name = "firma")
public class FirmaEntity extends BaseEntity {
private static final long serialVersionUID = -4105296181552840561L;
private Long id;
private String nazwa;
public FirmaEntity() {
}
public FirmaEntity(Long firmaId) {
this.id = firmaId;
}
@Id
@JsonView(ViewAlways.class)
@Cacheable("firmy")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonView(ViewAlways.class)
@Column(nullable = false)
public String getNazwa() {
return nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
@Override
public String toString() {
return "FirmaEntity{" + "id=" + id + ", nazwa=" + nazwa + '}';
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof FirmaEntity)) {
return false;
}
final FirmaEntity other = (FirmaEntity) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}
@Entity
@Table(name = "umowa_mpk")
public class UmowaMpkEntity implements Serializable {
private UmowaMpkId pk = new UmowaMpkId();
private Boolean aktywny = Boolean.TRUE;
private UmowaEntity umowa;
private MpkEntity mpk;
private FirmaEntity firma;
public UmowaMpkEntity() {
}
@EmbeddedId
public UmowaMpkId getPk() {
return pk;
}
public void setPk(UmowaMpkId pk) {
this.pk = pk;
}
@JsonView(UmowaView.MainView.class)
public Boolean getAktywny() {
return aktywny;
}
public void setAktywny(Boolean aktywny) {
this.aktywny = aktywny;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "umowa_id", nullable = false, insertable = false, updatable = false)
public UmowaEntity getUmowa() {
return umowa;
}
public void setUmowa(UmowaEntity umowa) {
this.umowa = umowa;
}
@ManyToOne(fetch = FetchType.LAZY)
@JsonView(UmowaView.MainView.class)
@JoinColumn(name = "mpk_id", nullable = false, insertable = false, updatable = false)
public MpkEntity getMpk() {
return mpk;
}
public void setMpk(MpkEntity mpk) {
this.mpk = mpk;
}
@ManyToOne(fetch = FetchType.LAZY)
@JsonView(UmowaView.MainView.class)
@JoinColumn(name = "firma_id", nullable = false, insertable = false, updatable = false)
public FirmaEntity getFirma() {
return firma;
}
public void setFirma(FirmaEntity firma) {
this.firma = firma;
}
@Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + Objects.hashCode(this.umowa.getId());
hash = 89 * hash + Objects.hashCode(this.mpk.getCompositeKey().getId());
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UmowaMpkEntity other = (UmowaMpkEntity) obj;
if (!Objects.equals(this.umowa.getId(), other.umowa.getId())) {
return false;
}
if (!Objects.equals(this.mpk.getCompositeKey().getId(), other.mpk.getCompositeKey().getId())) {
return false;
}
return true;
}
}
@Embeddable
public class UmowaMpkId implements Serializable {
private Long umowaId;
private CompositeKey compositeKey;
public UmowaMpkId() {
}
@Column(name = "umowa_id")
public Long getUmowaId() {
return umowaId;
}
public void setUmowaId(Long umowaId) {
this.umowaId = umowaId;
}
@Embedded
public CompositeKey getCompositeKey() {
return compositeKey;
}
public void setCompositeKey(CompositeKey compositeKey) {
this.compositeKey = compositeKey;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + Objects.hashCode(this.umowaId);
hash = 41 * hash + Objects.hashCode(this.compositeKey);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UmowaMpkId other = (UmowaMpkId) obj;
if (!Objects.equals(this.umowaId, other.umowaId)) {
return false;
}
if (!Objects.equals(this.compositeKey, other.compositeKey)) {
return false;
}
return true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.