I'm trying to create a join table with @JoinTable
from 3 different entities. Below is the code sample I'm using. While creating join table I'm getting below error. Please help to resolve.
There are 3 entities in my design. Credential, Category and Tenant. Now I'm trying to make a join table that will contain the pk of these 3 tables using @ManyToMany annotation between them. Below is the relationship which I'm trying to make.
@Entity
@Table(name = "CREDENTIALS")
public class CredentialsEntity {
/** The credential id. */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int credentialId;
@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "CATEGORY_HAS_CREDENTIALS",
joinColumns = {
@JoinColumn(table="CATEGORY", name = "CATEGORY_ID", referencedColumnName = "ID"),
@JoinColumn(table ="TENANT", name = "TENANT_ID", referencedColumnName = "ID")},
inverseJoinColumns = @JoinColumn(table="CREDENTIALS", name = "CREDENTIAL_ID", referencedColumnName = "ID"))
private List<TenantEntity> tenantEntities;
}
==========================
@Entity
@Table(name = "TENANT")
public class TenantEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int tenantId;
@ManyToMany(mappedBy = "tenantEntities", cascade = CascadeType.ALL)
private List<CredentialsEntity> credentialsEntities;
}
==========================
@Entity
@Table(name = "CATEGORY")
public class NodeCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int categoryId;
@ManyToMany(cascade = CascadeType.ALL)
private List<CredentialsEntity> credentialsEntities;
}
Caused by:
org.hibernate.AnnotationException: Cannot find the expected secondary table: no TENANT available for com.aricent.aricloud.entity.CredentialsEntity at org.hibernate.cfg.Ejb3Column.getJoin(Ejb3Column.java:363) at org.hibernate.cfg.Ejb3Column.getTable(Ejb3Column.java:342) at org.hibernate.cfg.Ejb3Column.checkPropertyConsistency(Ejb3Column.java:584) at org.hibernate.cfg.annotations.CollectionBinder.buildCollectionKey(CollectionBinder.java:1018) at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1336)
EDIT
I'm able to do the jointable like below, as mentioned in link
is this correct approach or I'm doing something wrong?
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name = "CATEGORY_HAS_CREDENTIALS",
joinColumns = {
@JoinColumn(name = "CREDENTIAL_ID", referencedColumnName = "ID")},
inverseJoinColumns = @JoinColumn(table = "CATEGORY", name = "CATEGORY_ID", referencedColumnName = "ID"))
@MapKeyJoinColumn(name = "TENANT_ID", referencedColumnName ="ID")
@ElementCollection
private Map<TenantEntity, NodeCategory> tenantCategoryMap = new HashMap<TenantEntity, NodeCategory>();