gpt4 book ai didi

java - 加入hibernate HQL

转载 作者:行者123 更新时间:2023-11-30 06:18:40 25 4
gpt4 key购买 nike

我有以下 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 中实现这个。作为初学者,我寻找一个简单易懂的答案。我无法理解之前的复杂实现。谢谢。包 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。然后我用它来填充我的数据表。我需要创建第三个实体吗?我不确定这是如何工作的,我一直在尝试加入但没有成功。

最佳答案

要以“hibernate 方式”执行此操作,您需要映射实体中的 tFrequencies 表,以从 tFrequencies 获取Value ,创建与tFrequencies的关系。

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

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

查询返回结果后,您可以通过tEXCELSMSTOSENDScheduletFrequencies访问Value:

tEXCELSMSTOSENDSchedule.getTFrequencies().getValue();

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

不返回整个实体

如果您不想返回整个实体,则可以使用Object array仅传递您想要的信息。 , Hibernate 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();

没有将表映射到实体

也可以在没有实体的情况下(使用 native SQL)做到这一点,通过 ResultTransformer ,也将信息传递给 DTO。

关于java - 加入hibernate HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com