У меня проблема с блоком SQL запроса и group by
внутри него.
Я использую SQL Server 2017 и Spring Boot.
Модель взаимодействия:
@Entity
@Table(name = "engagement")
public class EngagementModel extends AuditModel {
/**
*
*/
private static final long serialVersionUID = -4655229023870737943L;
@Id
@GeneratedValue(generator = "eng_generator")
@SequenceGenerator(
name = "eng_generator",
sequenceName = "eng_sequence",
initialValue = 1
)
@Column(name = "engagement_id", nullable = false)
private long engagementId;
@OneToMany(mappedBy = "engagementModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<ProdEngRelationModel> relations = new HashSet<>();
@OneToMany(mappedBy = "engagementModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<TaskModel> tasks = new HashSet<>();
@OneToMany(mappedBy = "engagementModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<NotificationModel> notifs = new HashSet<>();
@OneToMany(mappedBy = "engagementModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<EngagementUserIntModel> engUserInt = new HashSet<>();
@OneToMany(mappedBy = "engagementModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Set<EngagementUserExtModel> engUserExt = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@MapsId("client_id")
@JoinColumn(name = "clientId", referencedColumnName = "client_id")
private MasterClientModel masterClientModel;
private long clientId;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@MapsId("product_id")
@JoinColumn(name = "productId", referencedColumnName = "product_id")
private ListProductModel listProductModel;
private long productId;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@MapsId("status_id")
@JoinColumn(name = "statusId", referencedColumnName = "status_id")
private StatusModel statusModel;
private long statusId;
@Column(name = "previous_id")
private long previousId;
@Column(name = "client_code")
@Size(min = 1, max = 50)
private String clientCode;
@Column(name = "job_code")
@Size(min = 1, max = 25)
private String jobCode;
@Column(name = "job_desc")
@Size(min = 1, max = 255)
private String jobDesc;
@Column(name = "npwp")
@Size(min = 1, max = 15)
private String npwp;
@Column(name = "efin")
@Size(min = 1, max = 10)
private String efin;
@Column(name = "name")
@Size(min = 1, max = 65)
private String name;
@Column(name = "client_name")
@Size(min = 1, max = 65)
private String clientName;
@Column(name = "description")
@Size(min = 1, max = 255)
private String description;
@Column(name = "type")
@Size(min = 1, max = 25)
private String type;
@Column(name = "fiscal_year_start")
private String fiscalYearStart;
@Column(name = "fiscal_year_end")
private String fiscalYearEnd;
@Lob
@Type(type="org.hibernate.type.BinaryType")
@Column(name = "sertifikat_digital_efilling", columnDefinition="VARBINARY(MAX)")
private byte[] efilling;
@Lob
@Type(type="org.hibernate.type.BinaryType")
@Column(name = "sertifikat_digital_efaktur", columnDefinition="VARBINARY(MAX)")
private byte[] efaktur;
@Column(name = "archived")
private int archived;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "archived_date")
private Date archivedDate;
@Column(name = "is_deactivated")
private String isDeactivated;
private EngagementUserIntModel engagementUserIntModel;
public EngagementModel() {
}
// Getters and Setters (Omitted for brevity)
public long getEngagementId() {
return engagementId;
}
public void setEngagementId(long engagementId) {
this.engagementId = engagementId;
}
public long getPreviousId() {
return previousId;
}
public void setPreviousId(long previousId) {
this.previousId = previousId;
}
public String getClientCode() {
return clientCode;
}
public void setClientCode(String clientCode) {
this.clientCode = clientCode;
}
public String getJobCode() {
return jobCode;
}
public void setJobCode(String jobCode) {
this.jobCode = jobCode;
}
public String getJobDesc() {
return jobDesc;
}
public void setJobDesc(String jobDesc) {
this.jobDesc = jobDesc;
}
public String getNpwp() {
return npwp;
}
public void setNpwp(String npwp) {
this.npwp = npwp;
}
public String getEfin() {
return efin;
}
public void setEfin(String efin) {
this.efin = efin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFiscalYearStart() {
return fiscalYearStart;
}
public void setFiscalYearStart(String fiscalYearStart) {
this.fiscalYearStart = fiscalYearStart;
}
public String getFiscalYearEnd() {
return fiscalYearEnd;
}
public void setFiscalYearEnd(String fiscalYearEnd) {
this.fiscalYearEnd = fiscalYearEnd;
}
public byte[] getEfilling() {
return efilling;
}
public void setEfilling(byte[] efilling) {
this.efilling = efilling;
}
public byte[] getEfaktur() {
return efaktur;
}
public void setEfaktur(byte[] efaktur) {
this.efaktur = efaktur;
}
public int getArchived() {
return archived;
}
public void setArchived(int archived) {
this.archived = archived;
}
public Date getArchivedDate() {
return archivedDate;
}
public void setArchivedDate(Date archivedDate) {
this.archivedDate = archivedDate;
}
public MasterClientModel getMasterClientModel() {
return masterClientModel;
}
public void setMasterClientModel(MasterClientModel masterClientModel) {
this.masterClientModel = masterClientModel;
}
public ListProductModel getListProductModel() {
return listProductModel;
}
public void setListProductModel(ListProductModel listProductModel) {
this.listProductModel = listProductModel;
}
public StatusModel getStatusModel() {
return statusModel;
}
public void setStatusModel(StatusModel statusModel) {
this.statusModel = statusModel;
}
public long getClientId() {
return clientId;
}
public void setClientId(long clientId) {
this.clientId = clientId;
}
public long getProductId() {
return productId;
}
public void setProductId(long productId) {
this.productId = productId;
}
public long getStatusId() {
return statusId;
}
public void setStatusId(long statusId) {
this.statusId = statusId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getIsDeactivated() {
return isDeactivated;
}
public void setIsDeactivated(String isDeactivated) {
this.isDeactivated = isDeactivated;
}
public EngagementUserIntModel getEngagementUserIntModel() {
return engagementUserIntModel;
}
public void setEngagementUserIntModel(EngagementUserIntModel engagementUserIntModel) {
this.engagementUserIntModel = engagementUserIntModel;
}
}
Затем у меня есть настраиваемый запрос, как показано ниже, в моем репозитории:
select
MAX(eng.client_code) client_code,
MAX(eng.client_name) client_name,
MAX(eng.client_id) client_id
from engagement eng
where eng.is_deactivated = 'N'
group by eng.client_code, eng.client_name, eng.client_id;
Когда я запускаю этот запрос в моем SQL сервере-клиенте, он точно возвращает результат что я хочу. Но когда я тестирую его с помощью Postman, он выдает ошибку, как показано ниже в консоли:
Hibernate: select
MAX(eng.client_code) client_code,
MAX(eng.client_name) client_name,
MAX(eng.client_id) client_id
from engagement eng
where eng.is_deactivated = 'N'
group by eng.client_code, eng.client_name, eng.client_id;, eng.client_id
order by eng.client_name desc offset 0 rows
fetch next ? rows only
2020-07-09 15:13:54.827 WARN 2912 --- [io-10001-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper :
SQL Error: 0, SQLState: S1093
2020-07-09 15:13:54.827 ERROR 2912 --- [io-10001-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper :
The column name engagement_id is not valid.
2020-07-09 15:13:54.862 ERROR 2912 --- [io-10001-exec-5] o.s.t.i.TransactionInterceptor :
Application exception overridden by commit exception
Я понятия не имею, что не так в этом подходе. Любая помощь или предложения будут оценены. Спасибо.