I have following mappings
@Entity
@Table( name = "SECURITY_ZONES" )
public class SecurityZones implements Serializable {
@Id
@Column( name = "secZONE_ID", precision = 4 )
private Long securityZoneId;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "type_2_seczones",
joinColumns = @JoinColumn( name = "secZONE_ID", referencedColumnName = "secZONE_ID" ),
inverseJoinColumns = @JoinColumn( name = "SECTYPE_ID", referencedColumnName = "SECTYPE_ID" ) )
private Set<SecurityTypes> validSecTypes = new HashSet<>(0);
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "securityZone")
private Set<Type2Seczones> securityZones = new HashSet<>(0);
}
@Entity
@Table( name = "SECURITY_TYPES" )
public class SecurityTypes implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column( name = "SECTYPE_ID", precision = 4 )
private Long securityTypeId;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "type_2_seczones",
joinColumns = @JoinColumn( name = "SECTYPE_ID", referencedColumnName = "SECTYPE_ID" ),
inverseJoinColumns = @JoinColumn( name = "SECZONE_ID", referencedColumnName = "SECZONE_ID" ) )
private Set<SecurityZones> validSecZones = new HashSet<>(0);
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "securityType")
private Set<Type2Seczones> securityTypes = new HashSet<Type2Seczones>(0);
}
@Entity
@Table( name = "TYPE_2_SECZONES" )
public class Type2Seczones implements Serializable {
@Id
@Column( name = "RECORD_ID", length = 10 )
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name = "SECTYPE_ID", nullable = false )
@Fetch( FetchMode.SELECT )
private SecurityTypes securityType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name = "SECZONE_ID", nullable = false )
@Fetch( FetchMode.SELECT )
private SecurityZones securityZone;
}
@Entity
@Table( name = "TARGET_2_ACCESS" )
public class Target2Access implements Serializable {
@Id
@Column( name = "TARGET_ACCESS_ID", length = 15 )
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name = "TYPE_SECZONES_SOURCE_ID", referencedColumnName = "SECTYPE_ID", nullable = false )
@Fetch( FetchMode.SELECT )
private Type2Seczones sourceTypeZone ;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name = "TYPE_SECZONES_DEST_ID", referencedColumnName = "SECZONE_ID", nullable = false )
@Fetch( FetchMode.SELECT )
private Type2Seczones destTypeZone ;
}
I tried to draw ER diagram as follows
ER diagram
I think the mapping is correct but I am getting following mapping exception while trying this code
Caused by: org.hibernate.MappingException: Foreign key
(FK92nc9ujbcnmfiboi2hkmln6ij:TARGET_2_ACCESS [TYPE_SECZONES_DEST_ID]))
must have same number of columns as the referenced primary key
(TYPE_2_SECZONES [SECTYPE_ID,SECZONE_ID])
Please help if anyone find what am I doing wrong here.
Thanks in advance.
note – please ignore minor spelling mistakes if any I have edited variable names, but I have double checked in actual code there are no spelling mistakes or copy/paste errors