I want to display distinct year in combo box in ascending order. But the output in combox appears to be repetitive, repetitive in the sense that it shows all the elements of the column of database but i want to show only the distinct data of the column . What am i doing wrong?? I have simply done simple database query before in php. I know it is simply but i am not being to address the query in right order, i guess.
My code is as follows.
/*
* 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);
}
}