We are migrating to Composite Primary Key in our code as we are using Postgres which requires Partition Key to be part of Primary Key.
Below is our schema:
Company Entity
public class Company {
private Long id;
@Id
@Column(name = "COMPANY_SEQ")
public Long getId() {
return id;
}
}
Event Entity
public class Event implements Serializable {
private Long id = new Random().nextLong();
private Company company;
private Set<EventDetail> eventDetailSet;
private String eventType;
@Id
@Column(name = "EVENT_SEQ")
public Long getId() {
return id;
}
@JoinColumn(name = "COMPANY_FK")
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, optional = false)
@Id
public Company getCompany() {
return company;
}
@Column(name = "EVENT_TYPE")
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
@OneToMany(cascade = CascadeType.MERGE , mappedBy = "event" )
Set<EventDetail> getEventDetailSet()
{
return eventDetailSet;
}
}
EventDetail Entity
public class EventDetail implements Serializable {
private Long id;
private Company company;
private Event event;
@Id
@Column(name = "EVENT_DETAIL_SEQ")
public Long getId() {
return id;
}
@JoinColumn(name = "COMPANY_FK")
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, optional = true)
@Id
public Company getCompany() {
return company;
}
private Long eventSeq;
@Column(name = "EVENT_FK")
public Long getEventSeq() {
return eventSeq;
}
public void setEventSeq(Long eventSeq) {
this.eventSeq = eventSeq;
}
public void setEvent(Event event) {
this.event = event;
this.eventSeq = event == null ? null : event.getId();
}
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, optional = true)
@JoinColumns({
@JoinColumn(name = "EVENT_FK", referencedColumnName = "EVENT_SEQ", insertable = false, updatable = false),
@JoinColumn(name = "COMPANY_FK", referencedColumnName = "COMPANY_FK", insertable = false, updatable = false)
})
public Event getEvent() {
return event;
}
}
But on firing this hql query, we are getting wrong SQL Query generation.
HQL
select ed from EventDetail ed where ed.event.company = :company
SQL Generated:
select
ed1_0.COMPANY_FK,
ed1_0.EVENT_DETAIL_SEQ,
ed1_0.EVENT_FK
from
EventDetail ed1_0
where
ed1_0.COMPANY_FK=?
These sort of queries were working fine on Single Id, but starts behaving like this on moving to Composite ID.
Sample Test Case:
Branch: feature-composite-id-hql
I have shown both Single Id and Composite Id in the test case.
Can you please help to check this?