SELECT * FROM Emp_Main WHERE status = 'QUIT' AND to_Char(effective_date,'YYYY')= '2018';
I have hard coded '2018' in this example. But in the real code it is a variable :year
. Also I have Java EmpMain model like this :
@Entity
@Table(name="EMP_MAIN")
public class EmpMain implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_MAIN_SEQ")
@SequenceGenerator(sequenceName = "EMP_MAIN_S", allocationSize = 1, name = "EMP_MAIN_SEQ")
@Column(name="EMP_ID")
private long empId;
@Temporal(TemporalType.DATE)
@Column(name="EFFECTIVE_DATE")
private Date effectiveDate;
enter code here
@Column(name="STATUS")
private String status;
//Getters and Setters
}
Oh! I forgot to add my function I am trying to do....and here is the function I am playing with:
public List<EmpMain> getQuitEmployeesByYear(String inputYear)
{
List<EmpMain> listOfEmployees = null;
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<EmpMain> query = builder.createQuery(EmpMain.class);
Root<EmpMain> root = query.from(EmpMain.class);
Predicate statusPredicate = builder.equal(root.get("status"), "QUIT");
Expression<String> expression = builder.function(
"to_char", String.class,
root.get("effectiveDate"), "YYYY",
builder.parameter(String.class, inDate));
query.select(root).where(builder.and(statusPredicate, expression));
listOfEmployees = query.setParameter("inDate",
inputDate).getSingleResult();
return listOfEmployees;
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.