将PostgreSQL查询转换为相应的Hibernate查询

时间:2016-11-10 09:48:18

标签: java postgresql hibernate jpa java-ee

我需要PostgreSQL query.

的相应Hibernate查询

这是查询

select DATE(row_created) from DemoTable

其中DATE是PostgreSQL中的内置函数,而row_createdDemoTable中的数据类型为timestamp without time zone的列。

这是我的POJO课程

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "candidate")
public class Candidates { 
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int candidateId;

    @NotNull
    @Column(name = "name")
    private String name;

    @NotNull
    @Column(name = "mail_id")
    private String mailId;

    @NotNull
    @Column(name = "mobile_number")
    private String mobileNumber;

    @Column(name = "experience")
    private String experience;

    @Column(name = "ctc")
    private String ctc;

    @Column(name = "company")
    private String company;

    @NotNull
    @Column(name = "notice_period")
    private String noticePeriod;

    @Column(name = "prefered_job_location")
    private String preferedJobLocation;

    @Column(name = "resume_filepath_name")
    private String resumeFilepathName;

    @Column(name = "primary_skill")
    private String primarySkill;

    @NotNull
    @Column(name = "is_active")
    private Short isActive;

    @NotNull
    @Column(name = "row_created")
    private Date rowCreated;

    @Column(name = "row_altered")
    private Date rowAltered;

    @NotNull
    @ManyToOne
    @JoinColumn(name="tracker_fk_id")
    private Tracker trackerFk;

}

任何人都可以帮助获得相同的Hibernate查询。

由于

2 个答案:

答案 0 :(得分:0)

您可以像这样创建一个查询:

//Select your object that you want
Query query = em.createNamedQuery("SELECT a FROM DemoTable a WHERE mycondition");

1 - 如果要获得单一日期

//Get result list or a single result like what you want
DemoTableEntity myresult = (DemoTableEntity) query.getSingleResult();

//get the date from this result
Date mydate = myresult.getRowCreated();

2 - 如果要听取日期

List<Date> listeDate = new ArrayList<>();

List<DemoTableEntity> list = query.getResultList();

for(DemoTableEntity ent : list){
   listeDate.add(ent.getRowCreated());
}

3 - 如果要获取一列结果列表

Query query = em.createNamedQuery("SELECT a.rowAltered FROM Candidates a");
List<Date> dates = query.getResultList();

希望这可以帮到你。

答案 1 :(得分:0)

如果您需要检索数据,那么您可以按照“Youcef Laidani”的说法进行操作,这样您就可以在字段getter方法中修剪时间或在需要时修剪它。

但是如果你想在where条件下使用它,那么使用BETWEEN语句而不是等于

SELECT a FROM DemoTable WHERE a.row_created BETWEEN :from AND :to

目标日期为+1天

example values: 
from = 2016-11-10 00:00:00
to   = 2016-11-11 00:00:00
that means where created_date = 2016-11-10