I'm pretty new to Hibernate and after searching for a while, I still have problems with my annotations.
I have 3 classes:
I want a Many-To-Many relationship between AuftragsPosition and Material with an additional attribute on join table. The additional attribute menge should be accessable in AuftragsPosition . My Material should have no knowledge about the other two tables.
Here are my classes:
@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;
}
}
@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:
@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. 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.
You could create a join class ( AuftragsPositionMaterial
) that can contain the extra attribute menge
.
@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;
}
}
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.