Java的。如何返回扩展父项而不是父项列表的实体列表

时间:2014-11-14 11:03:28

标签: java oop inheritance reflection

我有通用的抽象类:

@MappedSuperclass
public abstract class Generic<T extends Generic> {
    @Transient
    public Class<T> entityClass;
    Generic() {
        entityClass = ((Class) ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
    }
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @ManyToOne(cascade = CascadeType.MERGE)
    @Constraints.Required
    public User creationUser;
    @Constraints.Required
    public String creationDate = Index.getDate(null);
    @Constraints.Required
    public String updateDate = Index.getDate(null);

    public User getCreationUser() {return creationUser;}
    public void setCreationUser(User user) {this.creationUser = user;}
    public void setCreationUser() {this.creationUser = User.getCurrentUser();}
    public String getCreationDate() {return creationDate;}
    public void setCreationDate(String creationDate) {this.creationDate = creationDate;}
    public String getUpdateDate() {return updateDate;}
    public void setUpdateDate(String updateDate) {this.updateDate = updateDate;}
    public T getBy(Long id) {
        return JPA.em().find(entityClass, id);
    }
    public List<T> getList() {
        Logger.debug("TEST");
        return (List<T>) JPA.em().createQuery("FROM " + entityClass.getSimpleName()).getResultList();
    }

    public List<T> getByUser_id(Long id) {
        List<T> entities = new ArrayList<T>();
        TypedQuery<T> query = JPA.em().createQuery("SELECT r FROM " + entityClass.getSimpleName() + " r WHERE r.user.id != :user_id", entityClass).setParameter("user_id", id);
        try {
            entities = query.getResultList();
        } catch (NoResultException e) {
            entities = null;
        }
        return entities;
    }



    @PrePersist
    public void prePersist() {
        Logger.warn(this.toString());
        setCreationDate(Index.getDate(null));
        preUpdate();
    }

    @PreUpdate
    public void preUpdate() {
        setUpdateDate(Index.getDate(null));
    }
    public void toDataBase() {
        JPA.em().persist(this);
    }
    public void update() {
        JPA.em().merge(this);
    }

    /**
     *  A Generic toString method that can be used in any class.
     *  uses reflection to dynamically print java class field
     *  values one line at a time.
     *  requires the Apache Commons ToStringBuilder class. 
     */
    public String toString() {
      return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}

哪个 getList 方法

此课程 extendet两次

Firt extend class是:

@MappedSuperclass
public abstract class GenericDictionary<T extends Generic> extends Generic<GenericDictionary> {

    @Required
    public String name;
    @Required
    public boolean active;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean stat) {
        this.active = stat;
    }
    /**
     * Options.
     *
     * @return the map
     */
    public Map<String, String> getOptions() {
        LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
        for (GenericDictionary e : getList()) {
            options.put(e.id.toString(), e.name);
        }
        return options;
    }


    public void on() {
        this.active = true;
        update();       
    }

    public void off() {
        this.active = false;
        update();       
    }
}

哪个 dosnt包含getList 方法

最后第二个扩展类是:

@Entity
@Table(name="common__Clinic")
public class Clinic extends GenericDictionary<Clinic> {
    @ManyToMany(cascade = CascadeType.PERSIST)
    public List<Unit> unit;
}

哪个还包含getList 方法

知道我想使用继承方法下载我的所有诊所:来自Generic类的getList。 我用这个代码:

new Clinic().getList()

但不幸的是我得到了:

List<GenericDictionary>

而不是

List<Clinic>

如何使用继承方法下载诊所列表?

2 个答案:

答案 0 :(得分:1)

更改

public abstract class GenericDictionary<T extends Generic> extends Generic<GenericDictionary> {

public abstract class GenericDictionary<T extends Generic> extends Generic<T> {

目前,Clinic延伸Generic<GenericDictionary> - 而不是Generic<Clinic>

答案 1 :(得分:0)

工作解决方案:

在控制器中,我从静态改变了方法:

Clinic.options()

动态:

new Clinic().getList()

在模型中,我改变了方法getOptions。在正文中,我添加了从getList()(返回Generic)到GenericDictionary的转换。这是完整的genericDictionary模型。

package models;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.MappedSuperclass;

import play.Logger;
import play.data.validation.Constraints;
import play.data.validation.Constraints.Required;
import play.db.jpa.JPA;

@MappedSuperclass
public abstract class GenericDictionary<T extends Generic<T>> extends Generic<T> {

    @Required
    public String name;
    @Required
    public boolean active;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean stat) {
        this.active = stat;
    }
    /**
     * Options.
     *
     * @return the map
     */
    public Map<String, String> getOptions() {
        LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
        for (T e : getList()) {
            GenericDictionary entity = (GenericDictionary) e;
            options.put(e.id.toString(), entity.name);
        }
        return options;
    }
//  @Override
//  public List<GenericDictionary> getList() {
//      return (List<GenericDictionary>) JPA.em().createQuery("FROM " + entityClass.getSimpleName()).getResultList();
//  }

    public void on() {
        this.active = true;
        update();       
    }

    public void off() {
        this.active = false;
        update();       
    }
}