I have a following problem that I need to solve. The core issues is that I want to add additional column into JoinTable for ManyToMany relation in JPA. In my case I have fol
One technique that is useful when creating the many-to-many mapping class entity is to attribute the id's in the class along with @ManyToOne designation which makes this class act as the composite key class:
@Entity
@Table(name = "market_vendor")
public class MarketVendor implements Serializable
{
@Id
@ManyToOne
@JoinColumn(name = "market_id")
private Market market;
@Id
@ManyToOne
@JoinColumn(name = "vendor_id")
private Vendor vendor;
@Basic
@Column(name="active")
private boolean active;
public MarketVendor(Market market, Vendor vendor, boolean active)
{
this.market = market;
this.vendor = vendor;
this.active = active;
}
}
This allows you to have the composite primary key defined within the same class without having to have a separate primary key class. You also need to make the class serializable.