I am facing below error when doing ManyToMany mapping with Composite Keys -
Caused by: javax.persistence.PersistenceException: [PersistenceUnit:
default] Unable to build Hibernate SessionFactory; nested exception is
org.hibernate.MappingException: Repeated column in mapping for
collection:
com.abc.reports.entities.Packages.protectedItems column:
account_id
I have gone through other sof post with similar error but nothing much help full for my case I could find there. This error has become a blocker for my release and so any help will be greatly appreciated.
Basically I have compound primary key in both the tables and have a join table to map the ManyToMany relationship.
My Entity classes and Embeddable classes are as below -
Compound Keys
@Embeddable
@Getter
@Setter
@ToString
public class PackageId implements Serializable {
private String accountId;
private Long packageId;
}
@Embeddable
@Getter
@Setter
@ToString
public class ProtectedItemsId implements Serializable {
private String accountId;
private Long protectedItemId;
}
Entity Classes.
Packages.class.
@Entity
@Getter
@Setter
@ToString
public class Packages {
@EmbeddedId
private PackageId packageId;
private String packageName;
@ManyToMany(mappedBy = "packageItemsList")
@JsonIgnore
private List<ProtectedItems> protectedItems;
}
ProtectedItems.class
@Entity
@Getter
@Setter
public class ProtectedItems implements Serializable {
@EmbeddedId
private ProtectedItemsId protectedItemId;
private String protectedItemName;
@ManyToMany
@JoinTable(
name="jt_packages_protected_items",
joinColumns = {@JoinColumn(name="accountId", insertable=false, updatable=false), @JoinColumn(name="protectedItemId", insertable=false, updatable=false)},
inverseJoinColumns = {@JoinColumn(name="accountId", insertable=false, updatable=false), @JoinColumn(name="packageId", insertable=false, updatable=false)} )
private List<Packages> packageItemsList;
@ManyToMany(mappedBy = "protectedItemsList")
@JsonIgnore
private List<StreamMappings> streamMappings;
@Override
public String toString() {
return "ProtectedItems{" +
"protectedItemId=" + protectedItemId +
", protectedItemName='" + protectedItemName +
", packageItemsList=" + packageItemsList.hashCode() +
", streamMappings=" + streamMappings +
'}';
}
}
The hibernate error message is little helpful in identifying the problem even after looking at the above classes.
I can add the DB schema if that helps, but think it’s not necessary. Like said any help will be greatly appreciated as it will let me proceed with my release schedule.