0

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.

enter image description here

@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>();
Community
  • 1
  • 1
bagui
  • 844
  • 4
  • 18
  • 37
  • How come that `CredentialsEntity` has both @ManyToOne and @ManyToMany association with `TenantEntity` at the same time? Am I missing something? – mmalik Jun 25 '15 at 20:11
  • `@ManyToOne` - join column in Credentials table for Tenant. `@ManyToMany` - to put the TenantEntity pk in new join table. The issue is in join table I cant pass table TENANT – bagui Jun 26 '15 at 08:34
  • errm, how does NodeCategory relate to CredentialsEntity.tenantEntities ? – Neil Stockton Jun 30 '15 at 07:42
  • @Neil - there is no direct relation between NodeCategory and CredentialsEntity.tenantEntities. But NodeCategory table does have a OneToMany relation with TenantEntity directly. – bagui Jun 30 '15 at 08:58
  • Simple answer : you CANNOT do that. – Neil Stockton Jun 30 '15 at 08:59
  • are you saying we cannot have join table for 3 different entities ? – bagui Jun 30 '15 at 09:13
  • you CANNOT do that. Can't get clearer. You have Credentials and Tenant relation yet no category present ... so it cannot insert something when it has no category! – Neil Stockton Jun 30 '15 at 09:37
  • I'm able to do this, please check the edit section. My concern is, will it create any issue while trying to insert values with Map in JoinTable ? – bagui Jul 01 '15 at 05:44

0 Answers0