2017-05-29 82 views
1

內容我有以下幾種方法:合併的方法

public ArrayList<Weapon> getDbWeapons() 
{ 
    ArrayList<Weapon> dbWeapons = new ArrayList<>();  

    EntityTransaction entr=em.getTransaction(); 
     entr.begin(); 
    TypedQuery<Weapon> query = em.createQuery("SELECT i FROM Weapon i", Weapon.class); 
    dbWeapons = new ArrayList<Weapon>(query.getResultList()); 
    em.getTransaction().commit(); 
    return dbWeapons; 
} 

public ArrayList<Armor> getDbArmors() 
{ 
    ArrayList<Armor> dbArmors = new ArrayList<>(); 

    EntityTransaction entr=em.getTransaction(); 
     entr.begin(); 
    TypedQuery<Armor> query = em.createQuery("SELECT i FROM Armor i", Armor.class); 
    dbArmors = new ArrayList<Armor>(query.getResultList()); 
    em.getTransaction().commit(); 
    return dbArmors; 
} 

public ArrayList<Potion> getDbPotions() 
{ 
    ArrayList<Potion> dbPotions = new ArrayList<>(); 
    EntityTransaction entr=em.getTransaction(); 
     entr.begin(); 
    TypedQuery<Potion> query = em.createQuery("SELECT i FROM Potion i", Potion.class); 
    dbPotions = new ArrayList<Potion>(query.getResultList()); 
    em.getTransaction().commit(); 
    return dbPotions; 

我的問題是 - 我怎麼第一次調用的方法到另一種方法(在同一個類),它們的內容合併到一個ArrayList中,然後返回它?

感謝您的回答!

類項目有(接口的新方法):

package com.dke.ps.Items; 

/** 
* General class for an item containing common information sutch as id, name, 
* description, path to an icon, price and type of an item. 
* @author valecvit 
* @author koresmi1 
*/ 
public abstract class Item 
{ 
    /** 
    * Unique id of an item. 
    */ 
    public int itemid; 
    /** 
    * Name of an item. 
    */ 
    public String name; 
    /** 
    * Description of an item. 
    */ 
    public String description; 
    /** 
    * Relative path to item image. 
    */ 
    public String icon; 
    /** 
    * Type of an item. 
    */ 
    public int type; 
    /** 
    * Price of an item. 
    */ 
    public int price; 

} 

我Table.Weapon類:

/* 
* 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 = "weapon") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Weapon.findAll", query = "SELECT w FROM Weapon w") 
    , @NamedQuery(name = "Weapon.findByItemid", query = "SELECT w FROM Weapon w WHERE w.itemid = :itemid") 
    , @NamedQuery(name = "Weapon.findByName", query = "SELECT w FROM Weapon w WHERE w.name = :name") 
    , @NamedQuery(name = "Weapon.findByDescription", query = "SELECT w FROM Weapon w WHERE w.description = :description") 
    , @NamedQuery(name = "Weapon.findByIcon", query = "SELECT w FROM Weapon w WHERE w.icon = :icon") 
    , @NamedQuery(name = "Weapon.findByType", query = "SELECT w FROM Weapon w WHERE w.type = :type") 
    , @NamedQuery(name = "Weapon.findByPower", query = "SELECT w FROM Weapon w WHERE w.power = :power") 
    , @NamedQuery(name = "Weapon.findByPrice", query = "SELECT w FROM Weapon w WHERE w.price = :price")}) 
public class Weapon 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 = "description") 
    private String description; 
    @Column(name = "icon") 
    private String icon; 
    @Column(name = "type") 
    private Integer type; 
    @Column(name = "power") 
    private Integer power; 
    @Column(name = "price") 
    private Integer price; 
    @JoinColumn(name = "itemid", referencedColumnName = "id", insertable = false, updatable = false) 
    @OneToOne(optional = false) 
    private ItemsId itemsId; 

    public Weapon() { 
    } 

    public Weapon(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 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 getPower() { 
     return power; 
    } 

    public void setPower(Integer power) { 
     this.power = power; 
    } 

    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 Weapon)) { 
      return false; 
     } 
     Weapon other = (Weapon) object; 
     if ((this.itemid == null && other.itemid != null) || (this.itemid != null && !this.itemid.equals(other.itemid))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "com.dke.ps.Tables.Weapon[ itemid=" + itemid + " ]"; 
    } 

} 

這是在包裝com.dke.ps.Items

第二類武器
package com.dke.ps.Items; 

/** 
* Class for a weapon. 
* @author valecvit 
* @author koresmi1 
*/ 
public class Weapon extends Item 
{ 
    /** 
    * Attack power of the weapon. 
    */ 
    public int power; 

    /** 
    * Initializes new weapon. 
    * @param id   unique item id 
    * @param name   name of item 
    * @param description description of item 
    * @param icon   path to item image 
    * @param price   price of item 
    * @param type   type of item 
    * @param power   power of item 
    */ 
    public Weapon(int id, String name, String description, String icon, int price, int type, int power) 
    { 
     this.itemid = id; 
     this.name = name; 
     this.description = description; 
     this.icon = icon; 
     this.price = price; 
     this.type = type; 
     this.power = power; 
    } 

} 
+0

也許一類'Player'有各自的'List'(S)的? –

+0

我剛更新了我原來的文章 – Aaka

+0

武器,護甲和藥水都擴展了Item嗎? –

回答

0

只需創建List<Item> getAllItems()方法並將所有項目添加到單個列表中,例如對於Weapon查詢:

public List<Item> getAllItems() { 
    List<Item> items = new ArrayList<Item>(); 

    // Database-related code 
    items.addAll(new ArrayList<Weapon>(query.getResultList())); 
    // Same for 2 remaining Item subclasses 
    return items; 
} 

也許有可能是沒有必要在ArrayListgetResultList(),檢查的getResultList()的文件中。

另外,您可以在單個事務中執行全部3個數據庫查詢。

+0

我不認爲你想在這三種方法中的每一種中創建一個新的ArrayList,是嗎?從你的回答中不清楚你應該如何以及在哪裏創建新列表。 – ajb

+0

有解決方案中的錯誤 - 「)」預計,找不到符號,符號:變量查詢,地點:類服務器,Reduntant類型的新的表達參數(採用金剛石操盤手) – Aaka

+0

有一個失蹤支架。您還應該添加用於處理數據庫查詢的相關代碼(與解決方案中的完全相同)。 – syntagma

0

您可以創建一個調用現有方法的方法:

public List<Item> getAllItems() 
{ 
    List<Item> items = new ArrayList<>(); 

    items.addAll(this.getDbWeapons()); 
    items.addAll(this.getDbArmors()); 
    items.addAll(this.getDbPotions()); 

    return items; 
} 
+0

這樣,你必須有3個單獨的交易,並創建3個不同的列表,當你真的需要1. – syntagma

+0

那麼正確的代碼應該是什麼樣子? – Aaka

+0

@ΔλЛ我不認爲我們需要關注自己在這一點上優化交易。爲什麼不重用OP已有的代碼,這是在3個單獨的事務中完成的。另外,對於調用代碼,它只是一個List。這與你的代碼有什麼不同,它也會將列表添加到返回的列表中? –