加入hibernate HQL

时间:2018-02-06 10:07:27

标签: java hibernate hql jpql

我有以下mysql查询:

SELECT s.file, sheet, s.messagetemplate,s.time_to_run,s.frequency,s.schedule_name, t.Value 
FROM  s   INNER JOIN  tFrequencies t ON s.frequencyId=t.id

我需要在hibernate中实现它。我寻找一个简单易懂的答案作为初学者。我不了解以前的复杂实现。谢谢。 package com.mspace1.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;    
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author support
 */
@Entity
@Table(name = "tFrequencies")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "TFrequencies.findAll", query = "SELECT t FROM TFrequencies t")
    , @NamedQuery(name = "TFrequencies.findById", query = "SELECT t FROM TFrequencies t WHERE t.id = :id")
    , @NamedQuery(name = "TFrequencies.findByValue", query = "SELECT t FROM TFrequencies t WHERE t.value = :value")
    , @NamedQuery(name = "TFrequencies.findByDescription", query = "SELECT t FROM TFrequencies t WHERE t.description = :description")})
public class TFrequencies implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 11)
    @Column(name = "Value")
    private String value;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "Description")
    private String description;

    private List <tEXCELSMSTOSENDSchedule> schedule;

    public TFrequencies() {
    }

    public TFrequencies(Integer id) {
        this.id = id;
    }

    public TFrequencies(Integer id, String value, String description) {
        this.id = id;
        this.value = value;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<tEXCELSMSTOSENDSchedule> getSchedule() {
        return schedule;
    }

    public void setSchedule(List<tEXCELSMSTOSENDSchedule> schedule) {
        this.schedule = schedule;
    }



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TFrequencies)) {
            return false;
        }
        TFrequencies other = (TFrequencies) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mspace1.model.TFrequencies[ id=" + id + " ]";
    }
    public static void main(String [] args){

    }
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mspace1.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author support
 */
@Entity
@Table(catalog = "dbSMS", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findAll", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findById", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.id = :id")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findBySheet", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.sheet = :sheet")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByMobileColumn", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.mobileColumn = :mobileColumn")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByTimeTosend", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.timeTosend = :timeTosend")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByUser", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.user = :user")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findBySource", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.source = :source")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByDeliverFrequency", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.deliverFrequency = :deliverFrequency")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByTimeToRun", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.timeToRun = :timeToRun")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByProcessed", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.processed = :processed")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByFrequency", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.frequency = :frequency")
    , @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByFrequencyId", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.frequencyId = :frequencyId")
, @NamedQuery(name = "tEXCELSMSTOSENDSchedule.findByFrequencyName", query = "SELECT t FROM tEXCELSMSTOSENDSchedule t WHERE t.frequencyName = :frequencyName")})
public class tEXCELSMSTOSENDSchedule implements Serializable {





    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)

    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(nullable = false, length = 65535)
    private String file;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(nullable = false, length = 50)
    private String sheet;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(nullable = false, length = 20)
    private String mobileColumn;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(nullable = false, length = 65535)
    private String messagetemplate;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date timeTosend;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(nullable = false, length = 20)
    private String user;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(nullable = false, length = 20)
    private String source;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private int deliverFrequency;
    @Column(name = "time_to_run")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timeToRun;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private short processed;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private short frequency;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private int frequencyId;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "schedule_name", nullable = false, length = 65535)
    private String scheduleName;
    private String frequencyName;
    public tEXCELSMSTOSENDSchedule() {
    }

    public tEXCELSMSTOSENDSchedule(Integer id) {
        this.id = id;
    }

    public tEXCELSMSTOSENDSchedule(Integer id, String file, String sheet, String mobileColumn, String messagetemplate, Date timeTosend, String user, String source, int deliverFrequency, short processed, short frequency, int frequencyId,String frequencyName, String scheduleName) {
        this.id = id;
        this.file = file;
        this.sheet = sheet;
        this.mobileColumn = mobileColumn;
        this.messagetemplate = messagetemplate;
        this.timeTosend = timeTosend;
        this.user = user;
        this.source = source;
        this.deliverFrequency = deliverFrequency;
        this.processed = processed;
        this.frequency = frequency;
        this.frequencyId = frequencyId;
        this.scheduleName = scheduleName;
        this.frequencyName=frequencyName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String getSheet() {
        return sheet;
    }

    public void setSheet(String sheet) {
        this.sheet = sheet;
    }

    public String getMobileColumn() {
        return mobileColumn;
    }

    public void setMobileColumn(String mobileColumn) {
        this.mobileColumn = mobileColumn;
    }

    public String getMessagetemplate() {
        return messagetemplate;
    }

    public void setMessagetemplate(String messagetemplate) {
        this.messagetemplate = messagetemplate;
    }

    public Date getTimeTosend() {
        return timeTosend;
    }

    public void setTimeTosend(Date timeTosend) {
        this.timeTosend = timeTosend;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public int getDeliverFrequency() {
        return deliverFrequency;
    }

    public void setDeliverFrequency(int deliverFrequency) {
        this.deliverFrequency = deliverFrequency;
    }

    public Date getTimeToRun() {
        return timeToRun;
    }

    public void setTimeToRun(Date timeToRun) {
        this.timeToRun = timeToRun;
    }

    public short getProcessed() {
        return processed;
    }

    public void setProcessed(short processed) {
        this.processed = processed;
    }

    public short getFrequency() {
        return frequency;
    }

    public void setFrequency(short frequency) {
        this.frequency = frequency;
    }

    public int getFrequencyId() {
        return frequencyId;
    }

    public void setFrequencyId(int frequencyId) {
        this.frequencyId = frequencyId;
    }

    public String getScheduleName() {
        return scheduleName;
    }

    public void setScheduleName(String scheduleName) {
        this.scheduleName = scheduleName;
    }

    public String getFrequencyName() {
        return frequencyName;
    }

    public void setFrequencyName(String frequencyName) {
        this.frequencyName = frequencyName;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof tEXCELSMSTOSENDSchedule)) {
            return false;
        }
        tEXCELSMSTOSENDSchedule other = (tEXCELSMSTOSENDSchedule) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mspace1.model.tEXCELSMSTOSENDSchedule[ id=" + id + " ]";
    }

}

我编辑了我的问题,添加了我正在使用的两个实体。第一个实体包含我想要与另一个实体中的选定项目连接的频率。 TFrequency表具有不会改变的常数值。此频率使用ID映射到tEXCELSMSTOSENDSchedule实体。但是,我想从TFrequency中选择名称而不是映射的id。然后我用它来填充我的数据表。我需要创建第三个实体吗?我不确定这是如何工作的,我一直试图加入,但没有成功。

1 个答案:

答案 0 :(得分:1)

要以&#34; hibernate方式&#34;执行此操作,您需要在实体中映射tFrequencies表以从Value获取tFrequencies,从而创建关系到tFrequencies

之后,如果您不想返回整个实体tEXCELSMSTOSENDSchedule,您可以这样做:

SELECT s FROM tEXCELSMSTOSENDSchedule s 
JOIN s.tFrequencies t
where t.id = :idFrequency

查询返回结果后,您可以ValuetFrequencies访问tEXCELSMSTOSENDSchedule

tEXCELSMSTOSENDSchedule.getTFrequencies().getValue();

提示:如果您使用的是Hibernate 5.1或更高版本,则需要don't need the relationship进行加入。

不返回整个实体

如果您不想返回整个实体,则只能使用Object arrayHibernate Tuplespecific Dto来传递您想要的信息。使用Dto,代码如下:

List<PostDTO> postDTOs = entityManager.createQuery(
"select new " +
"   com.páckage.ExcelDto("+
"       s.file, " +
"       s.sheet " + //TODO add the other attributes
"   ) " +
"from tEXCELSMSTOSENDSchedule s JOIN s.tFrequencies t" +
"where t.id = :idFrequency")
.setParameter("idFrequency", 1).getResultList();

不将表映射到实体

也可以通过ResultTransformer在没有实体(使用本机SQL)的情况下执行此操作,并将信息传递给DTO。