Saturday, February 5, 2011

jCombobox JPA HQL inner join error.

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 titles but showing something like Ljava.lang.object.xxxxx...

ERROR

Here is my codes ;

  if (jComboBox1.getSelectedItem() !=null) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SwingDenemePU");
    EntityManager em = emf.createEntityManager();
    Query sorgu = em.createQuery("from Personel p,Unvan u where  p.unvanID = u.unvanID and u.unvanID=:id");

int id = ((Unvan)jComboBox1.getSelectedItem()).getUnvanID();

   sorgu.setParameter("id", id);

   personelList = sorgu.getResultList();

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

    for (int i = 0; i < personelList.size(); i++) {
items[i] = personelList.get(i);
    }
    DefaultComboBoxModel def = new DefaultComboBoxModel(items);
    jComboBox2.setModel(def);

if i change items[i] = personelList.get(i) to ;

            Personel personel = personelList.get(i);
        items[i]=personel.getPersonelAdSoyad();

i am getting Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to DBClasses.Personel error.

  • The default combo box renderer simply invokes the toString() method of the Object contained in the model. So when you add a String to the model you see the value of the String because thats what the toString method returns.

    If you are storing a Personel Object in the model then you have two choices:

    a) add a toString() method to the Personel class b) create a custom renderer to display a property from the Personel class.

    Read the JComboBox API and you will find a link to the Swing tutorial on "How to Use Combo Boxes" which provides an example of a custom renderer.

    From camickr
  • you query appears incorrect, not sure what your mappings are but try something more like these:

        Query sorgu = em.createQuery("select p from Personel p,Unvan u where p.unvanID = u.unvanID and u.unvanID=:id");
    

    or

        Query sorgu = em.createQuery("from Personel p where p.unvanID=:id");
    
    Ibrahim AKGUN : thanx man u r solved my problem.
    From objects

0 comments:

Post a Comment