- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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
查询返回结果后,您可以通过tEXCELSMSTOSENDSchedule
从tFrequencies
访问Value
:
tEXCELSMSTOSENDSchedule.getTFrequencies().getValue();
提示:如果您使用的是 Hibernate 5.1 或更高版本,则don't need the relationship进行 JOIN。
如果您不想返回整个实体,则可以使用Object array仅传递您想要的信息。 , Hibernate Tuple或 specific 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/
我有实体和 NamedQuery: @Entity @Table(name="MY_TABLE") @NamedQueries({ @NamedQuery(name="myQuery", quer
hql 中的 row_number() 与分区的等价物是什么 我在 hql 中有以下查询: select s.Companyname, p.Productname, sum(od.Unitprice
hql 函数 current_timestamp() 使用的是来自运行 java 代码的服务器的时间还是来自运行数据库的服务器的时间? 最佳答案 HQL 查询被翻译成 SQL 查询,SQL 查询由数据
我有一个 Hibernate HQL 问题。 我想将子查询编写为派生表(出于性能原因)。 可以在 HQL 中做到这一点吗? 例子: FROM Customer WHERE country.id in
我有三个表 A B 和 C。现在我想在 HQL 中执行这个 sql 查询: select * from A as a left join B as b on a.id = b.id left join
我是 Hive 的新手,想知道如何直接从 .hql 文件执行 hive 命令。 最佳答案 如 @rajshukla4696 所述,hive -f filename.hql 或 beeline -f f
出于各种原因,我正在尝试编写一个部分动态的 HQL 查询,而不求助于 Criteria API。我想知道是否有一种简单的方法可以使用 HQL 表达式来短路 where 限制。例如,这是工作正常的原始查
我正在自学 hibernate ,并且很困惑为什么我不能只编写简单的 SQL 查询。 我发现它比普通 SQL(我习惯的)使用起来更困惑 PLUS:我发现 NetBeans HQL 编辑器非常烦人,对我
非常感谢您帮助将以下 SQL 语句转换为有效的 HQL 语句。我尝试了几个小时但没有成功: SELECT * FROM master as m left outer join (select * fr
我有一个 HQL 查询: query = select item.itemNumber from items item where item.stock>0 and item.price it = q
我在以下代码段中遇到错误 using (var session = Database.OpenSession()) { var q = from x in session.Query()
我正在使用以下 C# 代码: public IList GetAllByExpression(Expression> expression, int startIndex, int count, Fu
我有 HQL,我试图在其中获取没有分类的工件(当 Activity 为 0 时) artifacts = Artifact.findAll("FROM Artifact WHERE id NOT IN
在 JPQL 中,我可以通过以下方式检索实体: query = entityManager.createQuery("select c from Category c"); List categori
我正在尝试使用 HQL 查询更新记录,但我收到了 CastException。如果有人可以帮助我,我将不胜感激。我已经在网上查了一段时间,但我找不到这方面的任何信息。如果您有关于此异常的更多信息,请告
我正在尝试在 JPA 中执行一个 select 语句,这会抛出上述错误。代码是: TypedQuery typedQuery = null; String query = ""; List list
我有问题要执行一个更新 HQL 查询。 我想编辑保存在配置文件实体中的值,但没有运气。 我的实体如下所示 @Entity @Table(name = "users", catalog = "testd
如何设置此 hql 查询的限制?当我在查询中添加 limit 关键字时,会引发错误。 @Query("from voucher v where v.voucherType.typeDescript
假设有两个表,A[a_id, b_id]和 B[b_id,c] . 我需要执行表单 "From A a ORDER BY a.b.c" 的 HQL 查询, 而 b在类 A 中可以为空. 但是,该查询仅
是否可以在 HQL 中运行与此类似的查询而无需指定所有列名。 select med, MAX(med.PrescriptionLines.Prescription.PrescriptionDate)
我是一名优秀的程序员,十分优秀!