简体   繁体   中英

Embeddable class contains composite key generates too many columns?

I have two classes (UmowaEntity and MpkEntity). I replace the @ManyToMany association with two bidirectional @OneToMany associations because I need additional column ("aktywny"). MpkEntity contains composite key (CompositeKey).

MpkEntity:

@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;
    }
}

UmowaEntity:

 @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;
    }
}

CompositeKey:

@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;
    }
 }

FirmaEntity:

@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;
    }
}

I try to create @Embeddable class that contains composite key. I suspected that hibernate will create 4 columns (aktywny, umowa_id (pk), mpk_id (pk), firma_id (pk)) but I received 6 columns (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".

在此处输入图像描述

UmowaMpkEntity:

@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;
    }
}

I guess something is wrong with @MapsId at UnowaMpkEntity. It should be something like this

@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;
    }
}

Have a look at @MapsId documentation

PS Consider using English names for your classes

PPS Consider using Lombok for breviety

I would suggest you do not use associations within composite ids and instead map them in the entity only. Try this:

@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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM