gpt4 book ai didi

java - 在生成的 Restful Web 服务中获取更多查询

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

我使用 netbeans 创建一个 Restful Web 服务,我只需指定数据库和 netbeans 来创建所有内容。在子资源中,我只有 4 个 URI:

converter.city (http://localhost:8080/Data/resources/converter.city)

converter.city/{id}(http://localhost:8080/Data/resources/converter.city/{id})

converter.city/{from}/{to(http://localhost:8080/Data/resources/converter.city/{from}/{to})

converter.city/count(http://localhost:8080/Data/resources/converter.city/count)

这是生成的代码,观看第一部分

@Entity
@Table(name = "City")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "City.findAll", query = "SELECT c FROM City c"),
@NamedQuery(name = "City.findById", query = "SELECT c FROM City c WHERE c.id = :id"),
@NamedQuery(name = "City.findByName", query = "SELECT c FROM City c WHERE c.name = :name"),
@NamedQuery(name = "City.findByCountryCode", query = "SELECT c FROM City c WHERE c.countryCode = :countryCode"),
@NamedQuery(name = "City.findByDistrict", query = "SELECT c FROM City c WHERE c.district = :district"),
@NamedQuery(name = "City.findByPopulation", query = "SELECT c FROM City c WHERE c.population = :population")})
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 35)
@Column(name = "Name")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 3)
@Column(name = "CountryCode")
private String countryCode;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "District")
private String district;
@Basic(optional = false)
@NotNull
@Column(name = "Population")
private int population;

public City() {
}

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

public City(Integer id, String name, String countryCode, String district, int population) {
this.id = id;
this.name = name;
this.countryCode = countryCode;
this.district = district;
this.population = population;
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCountryCode() {
return countryCode;
}

public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}

public String getDistrict() {
return district;
}

public void setDistrict(String district) {
this.district = district;
}

public int getPopulation() {
return population;
}

public void setPopulation(int population) {
this.population = population;
}

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

在生成的代码中有很多查询,其中一个是我感兴趣的

 @NamedQuery(name = "City.findByCountryCode", query = "SELECT c FROM City c WHERE c.countryCode = :countryCode")    

如何使用 City.findByCountryCode ?

最佳答案

Query query = em.createNamedQuery("City.findByCountryCode")
.setParameter("countryCode", yourCountryCode);

关于java - 在生成的 Restful Web 服务中获取更多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384929/

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