I have a strange need in a project. Joining two n:m+attributes table (I will present the behavior with dummy attributes).
Of course that I have the tables Place, Address, School, Enterprise with theirs respective Ids, gets, sets and attributes implemented in the entity classes.
CODE:
Place
@Entity
@Table(name = "Place")
public class Place implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idLine")
private Long idLine;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.place")
private List<FirstTable> firstTables;
}
Address
@Entity
@Table(name = "Address")
public class Address implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idAddress")
private Long idAddress;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.address")
private List<FirstTable> firstTables;
}
School
@Entity
@Table(name = "School")
public class School implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idSchool")
private Long idSchool;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.school")
private List<FirstTable> firstTables;
}
FirstTable
@Entity
@Table(name = "FirstTable")
@AssociationOverrides({ @AssociationOverride(name = "pk.school", joinColumns = @JoinColumn(name = "idSchool")),
@AssociationOverride(name = "pk.address", joinColumns = @JoinColumn(name = "idAddress")),
@AssociationOverride(name = "pk.place", joinColumns = @JoinColumn(name = "idPlace")) })
public class FirstTable implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@EmbeddedId
protected FirstTablePK pk = new FirstTablePK();
}
FirstTablePK
@Embeddable
public class FirstTablePK implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@ManyToOne
private Address address;
@ManyToOne
private Place place;
@ManyToOne
private School school;
}
The above mentioned tables and joins are working perfectly. Now I want to join the FirstTable with the Second Table.
Enterprise
@Entity
@Table(name = "Enterprise")
public class Enterprise implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idEnterprise")
private Long idEnterprise;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.enterprise") private List secondTables; }
Now for the SecondTable I've followed the same logic to connect to the Enterprise. For connecting with the FirstTable I've tried this:
@Entity
@Table(name = "SecondTable")
@AssociationOverrides({
@AssociationOverride(name = "pk.firstTable", joinTable = @JoinTable(
name = "FirstTable", inverseJoinColumns = {
@JoinColumn(name = "idSchool", referencedColumnName = "idSchool"),
@JoinColumn(name = "idAddress", referencedColumnName = "idAddress"),
@JoinColumn(name = "idPlace", referencedColumnName = "idPlace") })),
@AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) })
public class SecondTable implements Serializable{}
Something is not working in my annotation, I'm trying to do an inverseJoin to the FirstTable table. The compilation shows this error:
"org.hibernate.AnnotationException: A component cannot hold properties split into 2 different tables"
I've tried to provide a MV example. Thanks in advance and I really need your help.
Hours later and many tries before I've managed to solve the problem. Actually the solution was much simpler that I was thinking initially.
Here it is:
@AssociationOverride(name = "pk.firstTable", joinColumns = {
@JoinColumn(name = "idSchool"),
@JoinColumn(name = "idAddress"),
@JoinColumn(name = "idPlace") }),
@AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.