简体   繁体   English

具有属性和单向映射的多对多关系

[英]Many-To-Many relationship with Attributes and unidirectional mapping

I'm pretty new to Hibernate and after searching for a while, I still have problems with my annotations. 我是Hibernate的新手,经过一段时间的搜索,我的注释仍然存在问题。

I have 3 classes: 我有3节课:

  1. AuftragsPosition AuftragsPosition
  2. AuftragspositionZuordung (join table) AuftragspositionZuordung(联接表)
  3. Material 材料

I want a Many-To-Many relationship between AuftragsPosition and Material with an additional attribute on join table. 我想要AuftragsPositionMaterial之间具有多对多关系,并在联接表上具有附加属性。 The additional attribute menge should be accessable in AuftragsPosition . 附加属性菜单应可在AuftragsPosition中访问。 My Material should have no knowledge about the other two tables. 我的资料应该不了解其他两个表。

Here are my classes: 这是我的课程:

AuftragsPosition AuftragsPosition

@Entity
@Table(name = "AUFTRAGSPOSITION")
public class AuftragsPosition extends AbstractVersionedEntity<Long> {

    private List<Material> materialien;

    @OneToMany(mappedBy = "position", targetEntity = AuftragspositionZuordnung.class, cascade = CascadeType.ALL)
    public List<Material> getMaterialien() {
        return materialien;
    }
}

AuftragspositionZuordnung 地位

@Entity
@Table(name = "ZUORDNUNG_AUFTRAGSPOSITION")
public class AuftragspositionZuordnung extends AbstractVersionedEntity<Long> {

    private AuftragsPosition position;

    private Material material;

    private Integer menge;

    @ManyToOne
    @JoinColumn(name = "AUFTRAGSPOSITION_ID")
    public AuftragsPosition getPosition() {
        return position;
    }

    @ManyToOne(targetEntity = Material.class)
    @JoinColumn(name = "MATERIAL_ID")
    public Material getMaterial() {
        return material;
    }

    @Column(name = "ANZAHL")
    public Integer getMenge() {
        return menge;
    }
}

Do you have any idea how I should annotate these classes? 您知道如何注释这些课程吗?

Thanks and kind regards, Kevin 谢谢你,凯文

I would add a map to the AuftragsPosition class looking approximately like this: 我会将地图添加到AuftragsPosition类中,看起来像这样:

@ElementCollection
@CollectionTable(name = "MENGEN",
        joinColumns = @JoinColumn(name = "AUFTRAGSPOS_ID"))
@MapKeyJoinColumn(name = "MATERIAL") 
@Column(name = "AMOUNT")
private Map<Material, Integer> amounts = new HashMap<Material, Integer>();

You need only the @ElementCollection annotation. 您只需要@ElementCollection批注。 I have included the others to demonstrate how you could manipulate the default table and column naming. 我已经包括了其他内容,以演示如何操作默认的表和列命名。

I have assumed that you want to key by Material , not by the amount - this would change the required annotations. 我假设您要按Material而不是按数量进行键输入-这将更改所需的注释。

You could create a join class ( AuftragsPositionMaterial ) that can contain the extra attribute menge . 您可以创建一个可以包含额外属性menge类( AuftragsPositionMaterial )。

@Entity
@Table(name = "AUFTRAGSPOSITION")
public class AuftragsPosition extends AbstractVersionedEntity<Long> {

    private Set<AuftragsPositionMaterial> auftragsPositionMaterials = new HashSet<AuftragsPositionMaterial>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.auftragsposition", cascade=CascadeType.ALL)
    public Set<AuftragsPositionMaterial> getAuftragsPositionMaterials() {
        return auftragsPositionMaterials;
    }
}

The Join Class: 联接类:

@Entity
@Table(name = "AuftragsPosition_Material")
@AssociationOverrides({
        @AssociationOverride(name = "pk.auftragsPosition", 
            joinColumns = @JoinColumn(name = "AUFTRAGSPOSITION_ID")),
        @AssociationOverride(name = "pk.material", 
            joinColumns = @JoinColumn(name = "MATERIAL_ID")) })
public class AuftragsPositionMaterial {

    private AuftragsPositionMaterialID pk = new AuftragsPositionMaterialID();

    @Column(name = "MENGE")
    private String menge;

    @EmbeddedId
    public AuftragsPositionMaterialID getPk() {
        return pk;
    }

}

Now the Primary Key: 现在,主键:

@Embeddable
public class AuftragsPositionMaterialID implements java.io.Serializable {

    private AuftragsPosition auftragsPosition;
    private Material material;

    @ManyToOne
    public Stock getAuftragsPosition() {
        return auftragsPosition;
    }

    public void setAuftragsPosition(AuftragsPosition auftragsPosition) {
        this.auftragsPosition= auftragsPosition;
    }

    @ManyToOne
    public Material getMaterial() {
        return material;
    }

    public void setMaterial(Material material) {
        this.material= material;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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