I have two date columns one is createDate and other is modifiedDate. Now I want to get max if these two date columns.
ProjectionList projectionList = Projections.projectionList();
projectionList.add( Projections.max("createdDate"));
How to add another date in Projection. and find max date of both. Not able to find any specific way to do this.
Entity is :-
package com.xyz.DemoEntity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.Formula;
@Entity
public class DemoEntity {
private Integer id;
private Date createdOn;
private Date modifiedOn;
@Formula("CASE WHEN createdOn > modifiedOn THEN createdOn ELSE modifiedOn END")
private Date maxDate;
/**
* @return the id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
@Column(updatable=false)
@Temporal(TemporalType.DATE)
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Temporal(TemporalType.DATE)
public Date getModifiedOn() {
return modifiedOn;
}
public void setModifiedOn(Date modifiedOn) {
this.modifiedOn = modifiedOn;
}
}
You should introduce an artificial column annotated with @Formula
Like this
@Formula("CASE WHEN createDate > modifiedDate THEN createDate ELSE modifiedDate END")
private Date maxDate;
And then use projectionList.add( Projections.max("maxDate"));
to get max.