gpt4 book ai didi

java - 在rest API中应用@OrderBy注释而不是排序

转载 作者:行者123 更新时间:2023-12-01 17:06:32 26 4
gpt4 key购买 nike

我正在尝试在 Rest API 中应用 @OrderBy 注释,我正在尝试按 styleName 对输出进行排序,但是,它不是按styleName 并且仍然按 id 顺序显示所有这些。我正在 postman 中测试这个。我如何错误地实现 @OrderBy 注释。

我用于@OrderBy的导入语句是

import javax.persistence.OrderBy;

我知道我可以创建一个像这样的新查询

@NamedQuery(name = "Styles.findAll", query = "SELECT s FROM Styles s ORDER BY s.name")

但是,我想使用@OrderBy注释

我将@Orderby注释放在Controller方法中,即 Controller

  @GetMapping(produces=MediaType.APPLICATION_JSON_VALUE)
@OrderBy("Styles.styleName DESC")
public List<Styles> GetAllStyles() {

return service.getAllStyles();
}

是否可以执行类似于服务和 Controller 的操作,而不是过滤数据,而是可以按列名称排序

服务是

  public List<Styles> getAllStyles() {
EntityManager em = DBUtil.getEMF().createEntityManager();

List<Styles> list = null;

try {
list = em.createNamedQuery("Styles.findAll", Styles.class)
.getResultList();
if (list == null || list.isEmpty()) {
list = null;
}

} finally {
em.close();
}
return list;

}

我使用的JPA是

@Entity
@Table(name = "styles")
@NamedQueries({
@NamedQuery(name = "Styles.findAll", query = "SELECT s FROM Styles s"),
@NamedQuery(name = "Styles.findById", query = "SELECT s FROM Styles s WHERE s.id = :id"),
@NamedQuery(name = "Styles.findByCatId", query = "SELECT s FROM Styles s WHERE s.catId = :catId"),
@NamedQuery(name = "Styles.findByStyleName", query = "SELECT s FROM Styles s WHERE s.styleName = :styleName"),
@NamedQuery(name = "Styles.findByLastMod", query = "SELECT s FROM Styles s WHERE s.lastMod = :lastMod")})
public class Styles 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
@Column(name = "cat_id")
private int catId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "style_name")
private String styleName;
@Basic(optional = false)
@NotNull
@Column(name = "last_mod")
@Temporal(TemporalType.TIMESTAMP)
private Date lastMod;

public Styles() {
}

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

public Styles(Integer id, int catId, String styleName, Date lastMod) {
this.id = id;
this.catId = catId;
this.styleName = styleName;
this.lastMod = lastMod;
}

public Integer getId() {
return id;
}

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

public int getCatId() {
return catId;
}

public void setCatId(int catId) {
this.catId = catId;
}

public String getStyleName() {
return styleName;
}

public void setStyleName(String styleName) {
this.styleName = styleName;
}

public Date getLastMod() {
return lastMod;
}

public void setLastMod(Date lastMod) {
this.lastMod = lastMod;
}

@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 Styles)) {
return false;
}
Styles other = (Styles) 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 "Service.Styles[ id=" + id + " ]";
}

最佳答案

由于您使用的是 javax.persistence.OrderBy,并遵循其文档,因此应该将其应用于 Entity 或 JPA(特别是 Spring)以使其正常工作

关于java - 在rest API中应用@OrderBy注释而不是排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61460759/

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