【问题标题】:Embeddable class contains composite key generates too many columns?可嵌入类包含复合键生成太多列?
【发布时间】:2021-04-20 08:46:58
【问题描述】:

我有两个类(UmowaEntity 和 MpkEntity)。我用两个双向@OneToMany 关联替换了@ManyToMany 关联,因为我需要额外的列(“aktywny”)。 MpkEntity 包含复合键(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;
    }
}

复合键:

@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 类。我怀疑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)。现在,当我尝试插入 UmowaMpkEntity 时,我得到了 UmowaMpkEntity 错误:“id”列中的空值违反了非空约束:由于“id”列,失败行包含 (null, t, 131, null, null, null)。

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

【问题讨论】:

    标签: hibernate many-to-many composite-primary-key hibernate-onetomany embeddable


    【解决方案1】:

    我猜 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;
        }
    }
    

    看看@MapsId documentation

    附:考虑为您的课程使用英文名称

    附言考虑使用Lombok 为简洁起见

    【讨论】:

    • 我尝试了您的解决方案,但出现异常:org.hibernate.MappingException:标识符映射的列数错误:pl.com.mggp.bz.uz.model.entity.UmowaMpkEntity 类型:组件[复合键,umowaId]
    【解决方案2】:

    我建议您不要在复合 ID 中使用关联,而是仅将它们映射到实体中。试试这个:

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

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多