title: JPA queries SELECT is not working
body: <p>I  have the following problem, I would like my application to list all the <code>Weapons</code> from the database. This is method <code>getDbWeapons</code>:</p>
  
  <pre><code> public ArrayList&lt;Weapon&gt; getDbWeapons()
      {
          ArrayList&lt;Weapon&gt; dbWeapons = new ArrayList&lt;&gt;();
          EntityTransaction entr=em.getTransaction();
              entr.begin();
          Query query = em.createQuery("SELECT i FROM Weapon i", Weapon.class);
          query.setParameter("itemid", dbWeapons);
          dbWeapons = (ArrayList&lt;Weapon&gt;) query.getResultList();
          em.getTransaction().commit();
  
          /*
          Stáhne všechny zbraně ze serveru a vrátí je jako ArrayList.
          Nezapomeň při vkládání doplnit parametr Item.type!
          */
          return dbWeapons;
      }
  </code></pre>
  
  <p>This is my class <code>Armor</code>:</p>
  
  <pre><code>/*
   * 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 com.dke.ps.Tables;
  
  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.JoinColumn;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  import javax.xml.bind.annotation.XmlRootElement;
  
  /**
   *
   * @author michal
   */
  @Entity
  @Table(name = "armor")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Armor.findAll", query = "SELECT a FROM Armor a")
      , @NamedQuery(name = "Armor.findByItemid", query = "SELECT a FROM Armor a WHERE a.itemid = :itemid")
      , @NamedQuery(name = "Armor.findByName", query = "SELECT a FROM Armor a WHERE a.name = :name")
      , @NamedQuery(name = "Armor.findByValue", query = "SELECT a FROM Armor a WHERE a.value = :value")
      , @NamedQuery(name = "Armor.findByDescription", query = "SELECT a FROM Armor a WHERE a.description = :description")
      , @NamedQuery(name = "Armor.findByIcon", query = "SELECT a FROM Armor a WHERE a.icon = :icon")
      , @NamedQuery(name = "Armor.findByType", query = "SELECT a FROM Armor a WHERE a.type = :type")
      , @NamedQuery(name = "Armor.findByDefence", query = "SELECT a FROM Armor a WHERE a.defence = :defence")
      , @NamedQuery(name = "Armor.findByPrice", query = "SELECT a FROM Armor a WHERE a.price = :price")})
  public class Armor implements Serializable {
  
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "itemid")
      private Integer itemid;
      @Column(name = "name")
      private String name;
      @Column(name = "value")
      private Integer value;
      @Column(name = "description")
      private String description;
      @Column(name = "icon")
      private String icon;
      @Column(name = "type")
      private Integer type;
      @Column(name = "defence")
      private Integer defence;
      @Column(name = "price")
      private Integer price;
      @JoinColumn(name = "itemid", referencedColumnName = "id", insertable = false, updatable = false)
      @OneToOne(optional = false)
      private ItemsId itemsId;
  
      public Armor() {
      }
  
      public Armor(Integer itemid) {
          this.itemid = itemid;
      }
  
      public Integer getItemid() {
          return itemid;
      }
  
      public void setItemid(Integer itemid) {
          this.itemid = itemid;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Integer getValue() {
          return value;
      }
  
      public void setValue(Integer value) {
          this.value = value;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String getIcon() {
          return icon;
      }
  
      public void setIcon(String icon) {
          this.icon = icon;
      }
  
      public Integer getType() {
          return type;
      }
  
      public void setType(Integer type) {
          this.type = type;
      }
  
      public Integer getDefence() {
          return defence;
      }
  
      public void setDefence(Integer defence) {
          this.defence = defence;
      }
  
      public Integer getPrice() {
          return price;
      }
  
      public void setPrice(Integer price) {
          this.price = price;
      }
  
      public ItemsId getItemsId() {
          return itemsId;
      }
  
      public void setItemsId(ItemsId itemsId) {
          this.itemsId = itemsId;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (itemid != null ? itemid.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 Armor)) {
              return false;
          }
          Armor other = (Armor) object;
          if ((this.itemid == null &amp;&amp; other.itemid != null) || (this.itemid != null &amp;&amp; !this.itemid.equals(other.itemid))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "com.dke.ps.Tables.Armor[ itemid=" + itemid + " ]";
      }
  
  }
  </code></pre>
  
  <p>And this is my class <code>Shop</code>, which should show the items: </p>
  
  <pre><code>package com.dke.ps.Shop;
  
  import static com.dke.ps.Constants.Constants.*;
  import com.dke.ps.CustomTypes.ImagePanel;
  import com.dke.ps.Items.Armor;
  import com.dke.ps.Items.Item;
  import com.dke.ps.Items.Potion;
  import com.dke.ps.Items.Weapon;
  import com.dke.ps.Server.*;
  import java.awt.event.WindowEvent;
  import java.util.ArrayList;
  import javax.swing.*;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  import net.miginfocom.swing.MigLayout;
  
  /**
   * Class implementing in-app-purchaces (microtransactions).
   * @author vitvalecka
   */
  public class Shop extends JFrame implements ListSelectionListener
  {
      private Server server;
      private User user;
  
      private JPanel panelShop;
      private JPanel panelItemDetail;
      private JPanel panelItemList;
      private JScrollPane spItemListScroll;
  
      private JList listItems;
      private DefaultListModel dlmItems = new DefaultListModel();
      private JButton btnBuy = new JButton("Buy");
      private JLabel lItemName = new JLabel();
      private ImagePanel ipItemImage;
      private JLabel lPrice = new JLabel();
      private JLabel lEffect = new JLabel();
      private JLabel lBought = new JLabel();
      private JButton btnReloadShop = new JButton("Reload Shop");
  
      private JPanel panelShopAdminButtons;
      private JButton btnAddItem = new JButton("Add");
      private JButton btnEditItem = new JButton("Edit");
      private JButton btnDeleteItem = new JButton("Delete");
  
      private ArrayList&lt;Item&gt; listOfDbItems = new ArrayList&lt;&gt;();
      private ArrayList&lt;Integer&gt; listOfUsersItems = new ArrayList&lt;&gt;();
  
      /**
       * Shop window initialization.
       * @param server    database server to connect
       * @param user      user entering the store
       */
      public Shop(Server server, User user)
      {
          super();
          this.server = server;
          this.user = user;
  
          init();
      }
  
      private void init()
      {
          dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
          setResizable(false);
          setTitle("Shop");
          setSize(SHOP_SIZE_X, SHOP_SIZE_Y);
  
          panelShop = new JPanel();
          setContentPane(panelShop);
          panelShop.setLayout(new MigLayout("", "[grow, fill]"));
  
          panelItemDetail = new JPanel();
          panelItemDetail.setLayout(new MigLayout(
                  "wrap",
                  "[grow, fill]"
          ));
  
          panelItemDetail.add(lItemName);
          panelItemDetail.add(lPrice);
          panelItemDetail.add(lEffect);
          panelItemDetail.add(lBought);
          panelItemDetail.add(btnBuy);
          btnBuy.addActionListener((e)-&gt;{
              buy();
          });
  
          if ((user.getUsername() == SHOP_ADMIN_USERNAME) &amp;&amp; (user.isLoginOk()))
          {
              panelShopAdminButtons = new JPanel();
              panelShopAdminButtons.setLayout(new MigLayout("", "[grow, fill]"));
              panelShopAdminButtons.add(btnAddItem);
              panelShopAdminButtons.add(btnEditItem);
              panelShopAdminButtons.add(btnDeleteItem);
  
              btnAddItem.addActionListener((e)-&gt;{
                  new AddEditItem(server, null);
              });
  
              btnEditItem.addActionListener((e)-&gt;{
                  if (getSelectedItem() != null)
                  {
                      new AddEditItem(server, getSelectedItem());
                  }
              });
  
              btnDeleteItem.addActionListener((e)-&gt;{
                  if (getSelectedItem() != null)
                  {
                      server.removeItemFromDb(getSelectedItem().id);
                  }
              });
          }
  
          panelItemList = new JPanel();
          panelItemList.setLayout(new MigLayout(
                  "wrap",
                  "[grow, fill]"
          ));
  
          loadItems();
          listItems = new JList(dlmItems);
          spItemListScroll = new JScrollPane(listItems);
          panelItemList.add(spItemListScroll);
          listItems.addListSelectionListener(this);
          listItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          listItems.setLayoutOrientation(JList.VERTICAL);
          listItems.setVisibleRowCount(VISIBLE_ITEMS);
          panelItemList.add(btnReloadShop);
          btnReloadShop.addActionListener((e)-&gt;{
              loadItems();
          });
  
          panelShop.add(panelItemList);
  
          ipItemImage = new ImagePanel("");
          String formatingString = "height " + ITEM_IMAGE_SIZE + ":" + ITEM_IMAGE_SIZE + ":" + ITEM_IMAGE_SIZE;
          panelItemList.add(ipItemImage, formatingString);
  
          panelItemList.add(panelItemDetail);
  
          btnReloadShop.addActionListener((e)-&gt;{loadItems();});
  
          setFocusableWindowState(false);
          setVisible(true);
          setFocusableWindowState(true);
      }
  
      private void loadItems()
      {
          listOfDbItems = server.getDbItems();
          listOfUsersItems = server.getPurchasedItems(user);
          dlmItems.clear();
  
          int numberOfItemsInDb = listOfDbItems.size();
  
          for (int i = 0; i &lt; numberOfItemsInDb; i++)
          {
              dlmItems.addElement((listOfDbItems.get(i)).name);
          }
      }
  
      private void buy()
      {
          int selectedIndex = listItems.getSelectedIndex();
          int selectedItemId = (listOfDbItems.get(selectedIndex)).id;
          server.purchaseItem(user, selectedItemId);
      }
  
      @Override
      public void valueChanged(ListSelectionEvent e) {
          if (e.getValueIsAdjusting() == false)
          {
              int selectedIndex = listItems.getSelectedIndex();
  
              if (selectedIndex &gt;= 0)
              {
  
                  Item selectedItem = listOfDbItems.get(selectedIndex);
  
                  lItemName.setText(selectedItem.name);
                  ipItemImage = new ImagePanel(selectedItem.icon);  
                  lPrice.setText("Price: " + selectedItem.price);
  
                  if (instancesInInventory(selectedItem) &gt; 0)
                  {
                      lBought.setText(
                              "You have already bought " +
                              instancesInInventory(selectedItem) +
                              " pieces."
                      );
                  }
                  else
                  {
                      lBought.setText("You have not bought this item yet.");
                  }
  
                  switch (selectedItem.type) {
                      case WEAPON_ID:
                      {
                          lEffect.setText("Power: " + ((Weapon)selectedItem).power);
                          break;
                      }
                      case ARMOR_ID:
                      {
                          lEffect.setText("Defence: " + ((Armor)selectedItem).defence);
                          break;
                      }
                      case POTION_ID:
                      {
                          lEffect.setText("Heals " + ((Potion)selectedItem).healRate + " points of health");
                          break;
                      }
                      default:
                          break;
  
                  }
              }
          }
      }
  
      private int instancesInInventory(Item selectedItem)
      {
          int numberOfInstances = 0;
  
          for (int i = 0; i &lt; listOfUsersItems.size(); i++)
          {
              if (selectedItem.id == listOfUsersItems.get(i))
              {
                  numberOfInstances++;
              }
          }
  
          return numberOfInstances;
      }
  
      private Item getSelectedItem()
      {
          int selectedIndex = listItems.getSelectedIndex();
          if (selectedIndex &gt;= 0)
              {
                  return listOfDbItems.get(selectedIndex);
              }
          return null;
      }
  }
  </code></pre>
  
  <p>Could you help me with this problem? Thanks a lot!</p>