jComboBox and JPA

View: New views
3 Messages — Rating Filter:   Alert me  

jComboBox and JPA

by thornZ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone ,



i am new at Java , i got a problem like this ;

i have got a desktop application , there is 2 jComboBox in JFrame.One of this jComboBox is hold Personels from Personel Table and another is take Personel's title.When jComboBox1's selected index changes occurs it will get personelid and fill jComboBox2 with its title.Thats so simple.But when selected index changes it filling with whole titles not only specific personel but also whole of them.



here is my codes ;







// TITLE CLASS



@Entity

@Table(name = "Unvan", catalog = "tksDB", schema = "dbo")

@NamedQueries({@NamedQuery(name = "Unvan.findAll", query

= "SELECT u FROM Unvan u"), @NamedQuery(name =

"Unvan.findByUnvanID", query = "SELECT u FROM Unvan u

WHERE u.unvanID = :unvanID"), @NamedQuery(name =

"Unvan.findByUnvanAdi", query = "SELECT u FROM Unvan u

WHERE u.unvanAdi = :unvanAdi")})

public class Unvan implements Serializable {

    @Transient

    private PropertyChangeSupport changeSupport = new

PropertyChangeSupport(this);

    private static final long serialVersionUID = 1L;

    @Id

    @Basic(optional = false)

    @Column(name = "UnvanID", nullable = false)

    private Integer unvanID;

    @Column(name = "UnvanAdi", length = 50)

    private String unvanAdi;

    @OneToMany(mappedBy = "unvanID")

    private Collection<Personel> personelCollection;



    public Unvan() {

    }



    public Unvan(Integer unvanID) {

        this.unvanID = unvanID;

    }



    public Integer getUnvanID() {

        return unvanID;

    }



    public void setUnvanID(Integer unvanID) {

        Integer oldUnvanID = this.unvanID;

        this.unvanID = unvanID;

        changeSupport.firePropertyChange("unvanID",

oldUnvanID, unvanID);

    }



    public String getUnvanAdi() {

        return unvanAdi;

    }



    public void setUnvanAdi(String unvanAdi) {

        String oldUnvanAdi = this.unvanAdi;

        this.unvanAdi = unvanAdi;

        changeSupport.firePropertyChange("unvanAdi",

oldUnvanAdi, unvanAdi);

    }



    public Collection<Personel> getPersonelCollection() {

        return personelCollection;

    }



    public void

setPersonelCollection(Collection<Personel>

personelCollection) {

        this.personelCollection = personelCollection;

    }



    @Override

    public int hashCode() {

        int hash = 0;

        hash += (unvanID != null ? unvanID.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 Unvan)) {

             return false;

        }

        Unvan other = (Unvan) object;

        if ((this.unvanID == null && other.unvanID !=

null) || (this.unvanID != null &&

!this.unvanID.equals(other.unvanID))) {

             return false;

        }

        return true;

    }



    @Override

    public String toString() {

        return unvanAdi;

    }



    public void

addPropertyChangeListener(PropertyChangeListener

listener) {

       

changeSupport.addPropertyChangeListener(listener);

    }



    public void

removePropertyChangeListener(PropertyChangeListener

listener) {

       

changeSupport.removePropertyChangeListener(listener);

    }



//PERSONEL CLASS



@Entity

@Table(name = "Personel", catalog = "tksDB", schema =

"dbo")

@NamedQueries({@NamedQuery(name = "Personel.findAll",

query = "SELECT p FROM Personel p"), @NamedQuery(name =

"Personel.findByPersonelID", query = "SELECT p FROM

Personel p WHERE p.personelID = :personelID"),

@NamedQuery(name = "Personel.findByPersonelAdSoyad",

query = "SELECT p FROM Personel p WHERE p.personelAdSoyad

= :personelAdSoyad")})

public class Personel implements Serializable {

    @Transient

    private PropertyChangeSupport changeSupport = new

PropertyChangeSupport(this);

    private static final long serialVersionUID = 1L;

    @Id

    @Basic(optional = false)

    @Column(name = "PersonelID", nullable = false)

    private Integer personelID;

    @Column(name = "PersonelAdSoyad", length = 50)

    private String personelAdSoyad;

    @JoinColumn(name = "UnvanID", referencedColumnName =

"UnvanID")

    @ManyToOne

    private Unvan unvanID;



    public Personel() {

    }



    public Personel(Integer personelID) {

        this.personelID = personelID;

    }



    public Integer getPersonelID() {

        return personelID;

    }



    public void setPersonelID(Integer personelID) {

        Integer oldPersonelID = this.personelID;

        this.personelID = personelID;

        changeSupport.firePropertyChange("personelID",

oldPersonelID, personelID);

    }



    public String getPersonelAdSoyad() {

        return personelAdSoyad;

    }



    public void setPersonelAdSoyad(String

personelAdSoyad) {

        String oldPersonelAdSoyad = this.personelAdSoyad;

        this.personelAdSoyad = personelAdSoyad;

       

changeSupport.firePropertyChange("personelAdSoyad",

oldPersonelAdSoyad, personelAdSoyad);

    }



    public Unvan getUnvanID() {

        return unvanID;

    }



    public void setUnvanID(Unvan unvanID) {

        Unvan oldUnvanID = this.unvanID;

        this.unvanID = unvanID;

        changeSupport.firePropertyChange("unvanID",

oldUnvanID, unvanID);

    }



    @Override

    public int hashCode() {

        int hash = 0;

        hash += (personelID != null ?

personelID.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 Personel)) {

             return false;

        }

        Personel other = (Personel) object;

        if ((this.personelID == null && other.personelID

!= null) || (this.personelID != null &&

!this.personelID.equals(other.personelID))) {

             return false;

        }

        return true;

    }



    @Override

    public String toString() {

        return personelAdSoyad;

    }



    public void

addPropertyChangeListener(PropertyChangeListener

listener) {

       

changeSupport.addPropertyChangeListener(listener);

    }



    public void

removePropertyChangeListener(PropertyChangeListener

listener) {

       

changeSupport.removePropertyChangeListener(listener);

    }



//// cODes when jCombobox actionevent fired



EntityManagerFactory emf =

Persistence.createEntityManagerFactory("SwingDenemePU");

        EntityManager em = emf.createEntityManager();

        Query sorgu = em.createQuery("select p from

Personel p where p.unvanID = :id");

       





        sorgu.setParameter("id",

((Unvan)jComboBox1.getSelectedItem()).getUnvanID());

       personelList = sorgu.getResultList();

       

        Object[] items = new Object[personelList.size()];

        for (int i = 0; i < personelList.size(); i++) {

             Personel personel = personelList.get(i);

             items=personel.getPersonelAdSoyad();



        }



        DefaultComboBoxModel def = new

      DefaultComboBoxModel(items);

        jComboBox2.setModel(def);





Re: jComboBox and JPA

by John Yeary :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Have you checked to make sure that you are getting the correct list of people in your query. You should run it in debug to see what list is returned from your query.  In general your code looks okay.

I would change the code to simplify it a little.

        personelList = sorgu.getResultList();

        if (!personelList.isEmpty()) {
            DefaultComboBoxModel def = new DefaultComboBoxModel(personelList.toArray());
            jComboBox2.setModel(def);
        }


On Sun, Jul 5, 2009 at 7:40 AM, ibrahimak <ibrahimak@...> wrote:
Hi everyone ,



i am new at Java , i got a problem like this ;

i have got a desktop application , there is 2 jComboBox in JFrame.One of this jComboBox is hold Personels from Personel Table and another is take Personel's title.When jComboBox1's selected index changes occurs it will get personelid and fill jComboBox2 with its title.Thats so simple.But when selected index changes it filling with whole titles not only specific personel but also whole of them.



here is my codes ;







// TITLE CLASS



@Entity

@Table(name = "Unvan", catalog = "tksDB", schema = "dbo")

@NamedQueries({@NamedQuery(name = "Unvan.findAll", query

= "SELECT u FROM Unvan u"), @NamedQuery(name =

"Unvan.findByUnvanID", query = "SELECT u FROM Unvan u

WHERE u.unvanID = :unvanID"), @NamedQuery(name =

"Unvan.findByUnvanAdi", query = "SELECT u FROM Unvan u

WHERE u.unvanAdi = :unvanAdi")})

public class Unvan implements Serializable {

   @Transient

   private PropertyChangeSupport changeSupport = new

PropertyChangeSupport(this);

   private static final long serialVersionUID = 1L;

   @Id

   @Basic(optional = false)

   @Column(name = "UnvanID", nullable = false)

   private Integer unvanID;

   @Column(name = "UnvanAdi", length = 50)

   private String unvanAdi;

   @OneToMany(mappedBy = "unvanID")

   private Collection<Personel> personelCollection;



   public Unvan() {

   }



   public Unvan(Integer unvanID) {

       this.unvanID = unvanID;

   }



   public Integer getUnvanID() {

       return unvanID;

   }



   public void setUnvanID(Integer unvanID) {

       Integer oldUnvanID = this.unvanID;

       this.unvanID = unvanID;

       changeSupport.firePropertyChange("unvanID",

oldUnvanID, unvanID);

   }



   public String getUnvanAdi() {

       return unvanAdi;

   }



   public void setUnvanAdi(String unvanAdi) {

       String oldUnvanAdi = this.unvanAdi;

       this.unvanAdi = unvanAdi;

       changeSupport.firePropertyChange("unvanAdi",

oldUnvanAdi, unvanAdi);

   }



   public Collection<Personel> getPersonelCollection() {

       return personelCollection;

   }



   public void

setPersonelCollection(Collection<Personel>

personelCollection) {

       this.personelCollection = personelCollection;

   }



   @Override

   public int hashCode() {

       int hash = 0;

       hash += (unvanID != null ? unvanID.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 Unvan)) {

            return false;

       }

       Unvan other = (Unvan) object;

       if ((this.unvanID == null && other.unvanID !=

null) || (this.unvanID != null &&

!this.unvanID.equals(other.unvanID))) {

            return false;

       }

       return true;

   }



   @Override

   public String toString() {

       return unvanAdi;

   }



   public void

addPropertyChangeListener(PropertyChangeListener

listener) {



changeSupport.addPropertyChangeListener(listener);

   }



   public void

removePropertyChangeListener(PropertyChangeListener

listener) {



changeSupport.removePropertyChangeListener(listener);

   }



//PERSONEL CLASS



@Entity

@Table(name = "Personel", catalog = "tksDB", schema =

"dbo")

@NamedQueries({@NamedQuery(name = "Personel.findAll",

query = "SELECT p FROM Personel p"), @NamedQuery(name =

"Personel.findByPersonelID", query = "SELECT p FROM

Personel p WHERE p.personelID = :personelID"),

@NamedQuery(name = "Personel.findByPersonelAdSoyad",

query = "SELECT p FROM Personel p WHERE p.personelAdSoyad

= :personelAdSoyad")})

public class Personel implements Serializable {

   @Transient

   private PropertyChangeSupport changeSupport = new

PropertyChangeSupport(this);

   private static final long serialVersionUID = 1L;

   @Id

   @Basic(optional = false)

   @Column(name = "PersonelID", nullable = false)

   private Integer personelID;

   @Column(name = "PersonelAdSoyad", length = 50)

   private String personelAdSoyad;

   @JoinColumn(name = "UnvanID", referencedColumnName =

"UnvanID")

   @ManyToOne

   private Unvan unvanID;



   public Personel() {

   }



   public Personel(Integer personelID) {

       this.personelID = personelID;

   }



   public Integer getPersonelID() {

       return personelID;

   }



   public void setPersonelID(Integer personelID) {

       Integer oldPersonelID = this.personelID;

       this.personelID = personelID;

       changeSupport.firePropertyChange("personelID",

oldPersonelID, personelID);

   }



   public String getPersonelAdSoyad() {

       return personelAdSoyad;

   }



   public void setPersonelAdSoyad(String

personelAdSoyad) {

       String oldPersonelAdSoyad = this.personelAdSoyad;

       this.personelAdSoyad = personelAdSoyad;



changeSupport.firePropertyChange("personelAdSoyad",

oldPersonelAdSoyad, personelAdSoyad);

   }



   public Unvan getUnvanID() {

       return unvanID;

   }



   public void setUnvanID(Unvan unvanID) {

       Unvan oldUnvanID = this.unvanID;

       this.unvanID = unvanID;

       changeSupport.firePropertyChange("unvanID",

oldUnvanID, unvanID);

   }



   @Override

   public int hashCode() {

       int hash = 0;

       hash += (personelID != null ?

personelID.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 Personel)) {

            return false;

       }

       Personel other = (Personel) object;

       if ((this.personelID == null && other.personelID

!= null) || (this.personelID != null &&

!this.personelID.equals(other.personelID))) {

            return false;

       }

       return true;

   }



   @Override

   public String toString() {

       return personelAdSoyad;

   }



   public void

addPropertyChangeListener(PropertyChangeListener

listener) {



changeSupport.addPropertyChangeListener(listener);

   }



   public void

removePropertyChangeListener(PropertyChangeListener

listener) {



changeSupport.removePropertyChangeListener(listener);

   }



//// cODes when jCombobox actionevent fired



EntityManagerFactory emf =

Persistence.createEntityManagerFactory("SwingDenemePU");

       EntityManager em = emf.createEntityManager();

       Query sorgu = em.createQuery("select p from

Personel p where p.unvanID = :id");







       sorgu.setParameter("id",

((Unvan)jComboBox1.getSelectedItem()).getUnvanID());

      personelList = sorgu.getResultList();



       Object[] items = new Object[personelList.size()];

       for (int i = 0; i < personelList.size(); i++) {

            Personel personel = personelList.get(i);

            items=personel.getPersonelAdSoyad();



       }



       DefaultComboBoxModel def = new

     DefaultComboBoxModel(items);

       jComboBox2.setModel(def);







--
John Yeary
--
http://javaevangelist.blogspot.com

"Far better it is to dare mighty things, to win glorious triumphs, even though checkered by failure, than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the grey twilight that knows not victory nor defeat."
-- Theodore Roosevelt

jComboBox and JPA

by thornZ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yes i got checked it.Its getting whole personels not only the specific personel.