合并方法的内容

时间:2017-05-29 23:23:54

标签: java

我有以下方法:

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;
    }

}

2 个答案:

答案 0 :(得分: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;
}

可能没有必要在getResultList()中包含ArrayList,请在getResultList()的文档中查看。

此外,您可以在一次交易中完成所有3个数据库查询。

答案 1 :(得分:0)

您可以创建一个调用现有方法的方法:

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

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

    return items;
}