6

I have the following situation:

I´m trying to build an application which is multi-tenant with the same tenants in one database with the same tables. As by know Hibernate is not supporting this variant before 5.0 as I found.

I`m trying to solve this by adding a brandId field to every table.

As I build Many To Many relationships I also added this brandId to the ManyToMany Join Table and here (dont know if I can do this, mysql is not complaining) I made a foreign key to both tables while both include the brandid

So now for example I have a table Text(ID,name,brandId) and a Tag(ID,name,brandId) and a join table (text_id,tag_id,brand_id) where the foreign keys are

CONSTRAINT FK_TAG_TEXTS_TAG FOREIGN KEY (TAG_ID,BRAND_ID) REFERENCES TAG (ID,brand),

CONSTRAINT FK_TAG_TEXTS_TEXT FOREIGN KEY (TEXT_ID,BRAND_ID) REFERENCES TEXT (ID,brand)

As you can see Brand ID is used twice.

Then I generated my classes with Hibernate Tools, which created a Composite Primary Key Class as it should and the association in the Tag Class.

  @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.PERSIST,CascadeType.MERGE })
  @JoinTable(name = "tag_texts", , joinColumns = {
    @JoinColumn(name = "TAG_ID", nullable = false, insertable = false, updatable = false),
    @JoinColumn(name = "BRAND_ID", nullable = false, insertable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "TEXT_ID", insertable = false, nullable = false, updatable = false),@JoinColumn( name = "BRAND_ID",  insertable = false, nullable = false, updatable = false) })
public List<Text> getTexts() {
    return this.texts;
}

The problem is now that I get the following exception:

org.hibernate.MappingException: Repeated column in mapping for collection: de.company.domain.Tag.texts column: brand_id

I looked into the Hibernate code in the Collection class which raises the exception. Here a method 'checkColumnDupliation' is called which uses a Set and inserts the name, what means that a second time inserting "BRAND_ID" as column leads to this behaviour.

As I found the most common solution for the Repeated column error is by inserting 'insertable = false and updateable = false' when using the same column in several references. This is described here:
Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

But this seems to be not the same problem as mine.

So my question is: Is there a possibility to fix this with the JPA Annotations and use the Brand ID in both joinColumns and inverseJoinColumns?

Community
  • 1
  • 1
Logarith
  • 690
  • 1
  • 7
  • 27

1 Answers1

0

The problem is that you want a JoinTable between 3 entities: Text, Tag and Brand.

Probably you will have to use an IdClass, something like :

public class AssociationId implements Serializable {
  private long textId;
  private long tagId;
  private long brandId;

  hash and equals function
  ...
}

Id Class Entity:

@Entity
@Table(name="tag_text_brand")
@IdClass(AssociationId.class)
public class TagTextBrandAssociation {
  @Id
  private long tagId;
  @Id
  private long textId;
  @Id
  private long textId;

  @ManyToOne
  @PrimaryKeyJoinColumn(name="TAG_ID", referencedColumnName="ID")
  private Tag tag;
  @ManyToOne
  @PrimaryKeyJoinColumn(name="TEXT_ID", referencedColumnName="ID")
  private Text text;
  @ManyToOne
  @PrimaryKeyJoinColumn(name="BRAND_ID", referencedColumnName="ID")
  private Brand brand;
  ...
}

You can use this in your 3 entities like this:

@Entity
public class Text {
  @Id
  private long id;
  ...
  @OneToMany(mappedBy="text")
  private List<TagTextBrandAssociation> tagsAndBrands;
  ...
}

See here for more information.

K.C.
  • 2,084
  • 2
  • 25
  • 38