我想以升序显示组合框中的不同年份。但是combox中的输出似乎是重复的,重复的,因为它显示了数据库列的所有元素,但我想只显示列的不同数据。我究竟做错了什么??我之前在php中做过简单的数据库查询。我知道这很简单但我不是以正确的顺序解决查询,我猜。
我的代码如下。
/*
* 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 my_ui;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
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.persistence.Transient;
/**
*
* @author enjal
*/
@Entity
@Table(name = "production", catalog = "data2", schema = "")
@NamedQueries({
@NamedQuery(name = "Production.findAll", query = "SELECT p FROM Production p"),
@NamedQuery(name = "Production.findByProductionId", query = "SELECT p FROM Production p WHERE p.productionId = :productionId"),
@NamedQuery(name = "Production.findByCropId", query = "SELECT p FROM Production p WHERE p.cropId = :cropId"),
@NamedQuery(name = "Production.findByLocationId", query = "SELECT p FROM Production p WHERE p.locationId = :locationId"),
@NamedQuery(name = "Production.findByArea", query = "SELECT p FROM Production p WHERE p.area = :area"),
@NamedQuery(name = "Production.findByProductionAmount", query = "SELECT p FROM Production p WHERE p.productionAmount = :productionAmount"),
@NamedQuery(name = "Production.findByYieldAmount", query = "SELECT p FROM Production p WHERE p.yieldAmount = :yieldAmount"),
@NamedQuery(name = "Production.findByYearOfProduction", query = "SELECT DISTINCT p FROM Production p WHERE p.yearOfProduction = :yearOfProduction ORDER BY p.yearOfProduction ASC" )})
//SELECT DISTINCT year_of_production FROM `production` ORDER BY year_of_production ASC
public class Production implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "production_id")
private Integer productionId;
@Basic(optional = false)
@Column(name = "crop_id")
private int cropId;
@Basic(optional = false)
@Column(name = "location_id")
private String locationId;
@Basic(optional = false)
@Column(name = "area")
private double area;
@Basic(optional = false)
@Column(name = "production_amount")
private String productionAmount;
@Basic(optional = false)
@Column(name = "yield_amount")
private double yieldAmount;
@Basic(optional = false)
@Column(name = "year_of_production")
private String yearOfProduction;
public Production() {
}
public Production(Integer productionId) {
this.productionId = productionId;
}
public Production(Integer productionId, int cropId, String locationId, double area, String productionAmount, double yieldAmount, String yearOfProduction) {
this.productionId = productionId;
this.cropId = cropId;
this.locationId = locationId;
this.area = area;
this.productionAmount = productionAmount;
this.yieldAmount = yieldAmount;
this.yearOfProduction = yearOfProduction;
}
public Integer getProductionId() {
return productionId;
}
public void setProductionId(Integer productionId) {
Integer oldProductionId = this.productionId;
this.productionId = productionId;
changeSupport.firePropertyChange("productionId", oldProductionId, productionId);
}
public int getCropId() {
return cropId;
}
public void setCropId(int cropId) {
int oldCropId = this.cropId;
this.cropId = cropId;
changeSupport.firePropertyChange("cropId", oldCropId, cropId);
}
public String getLocationId() {
return locationId;
}
public void setLocationId(String locationId) {
String oldLocationId = this.locationId;
this.locationId = locationId;
changeSupport.firePropertyChange("locationId", oldLocationId, locationId);
}
public double getArea() {
return area;
}
public void setArea(double area) {
double oldArea = this.area;
this.area = area;
changeSupport.firePropertyChange("area", oldArea, area);
}
public String getProductionAmount() {
return productionAmount;
}
public void setProductionAmount(String productionAmount) {
String oldProductionAmount = this.productionAmount;
this.productionAmount = productionAmount;
changeSupport.firePropertyChange("productionAmount", oldProductionAmount, productionAmount);
}
public double getYieldAmount() {
return yieldAmount;
}
public void setYieldAmount(double yieldAmount) {
double oldYieldAmount = this.yieldAmount;
this.yieldAmount = yieldAmount;
changeSupport.firePropertyChange("yieldAmount", oldYieldAmount, yieldAmount);
}
public String getYearOfProduction() {
return yearOfProduction;
}
public void setYearOfProduction(String yearOfProduction) {
String oldYearOfProduction = this.yearOfProduction;
this.yearOfProduction = yearOfProduction;
changeSupport.firePropertyChange("yearOfProduction", oldYearOfProduction, yearOfProduction);
}
@Override
public int hashCode() {
int hash = 0;
hash += (productionId != null ? productionId.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 Production)) {
return false;
}
Production other = (Production) object;
if ((this.productionId == null && other.productionId != null) || (this.productionId != null && !this.productionId.equals(other.productionId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "my_ui.Production[ productionId=" + yearOfProduction + " ]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
答案 0 :(得分:1)
我想以升序显示组合框中的不同年份。但是组合框中的输出似乎是重复的,重复的,因为它显示了数据库列的所有元素,但我只想显示列的不同数据
你说这是你用来完成的查询
@NamedQuery(name = "Production.findByYearOfProduction",
query = "SELECT DISTINCT p FROM Production p
WHERE p.yearOfProduction = :yearOfProduction
ORDER Bp.yearOfProduction ASC" )})
此查询的问题在于它用于通过yearOfProduction查找生产实体列表。所以说你在1990年使用这个查询。这意味着1990年生产的任何生产实体都是结果集的一部分。因此,您将看到的唯一生产年份是1990年。
您想要的只是单个列中的不同值,而实际的生产实体并不一定关注。所以你的查询看起来应该更像
SELECT DISTINCT p.yearOfProduction
FROM Production p
ORDER BY p.yearOfProduction ASC
返回值应该只是不同年份的列表,并且与Production实体没有任何关联。您可能希望对代码进行一些重构,调用此查询将返回List<String>
。这就是您要用于组合框的列表,而不是List<Production>
。你应该在哪里进行这种重构?我无法告诉您,因为您没有提供调用此查询的代码。
注意:那么你应该做的是创建一个单独的@NamedQuery,以及你现在拥有的那个,我想你想要保留一个不同的组合框,从第一个组合框中选择一年后,第二个是当年的所有生产实体。另请注意,您的toString仅列出生产年份。因此,如果您想要在第二个组合框中对Production实体进行其他表示,您也应该更改它。
在Netbeans中自动创建实体时,请记住,它将为实体的每个属性创建findAll
查询和查询findByXxx
查询。就是这样。所有查询都是使用一个 属性作为查找匹配结果的参数来返回生产实体列表。您需要的任何其他返回类型(在本例中为字符串列表)或不同的参数查询,您需要创建自定义查询