JAVA如何返回扩展父级而不是父级列表的实体列表
我有一个通用抽象类:JAVA如何返回扩展父级而不是父级列表的实体列表,java,oop,inheritance,reflection,Java,Oop,Inheritance,Reflection,我有一个通用抽象类: @MappedSuperclass public abstract class Generic<T extends Generic> { @Transient public Class<T> entityClass; Generic() { entityClass = ((Class) ((Class) ((ParameterizedType) getClass().getGenericSuperclass()
@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);
}
}
但不幸的是,我得到了:
List<GenericDictionary>
列表
而不是
List<Clinic>
列表
如何使用继承的方法下载诊所列表?更改
public abstract class GenericDictionary<T extends Generic> extends Generic<GenericDictionary> {
公共抽象类GenericDictionary扩展了Generic{
到
公共抽象类GenericDictionary扩展了Generic{
从目前的情况看,Clinic
扩展Generic
-而不是Generic
工作解决方案:
在控制器中,我已将方法从静态更改为:
Clinic.options()
要动态:
new Clinic().getList()
在模型中,我更改了方法getOptions。在主体中,我添加了从getList()(返回泛型)到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();
}
}
封装模型;
导入java.util.LinkedHashMap;
导入java.util.List;
导入java.util.Map;
导入javax.persistence.MappedSuperclass;
导入play.Logger;
导入play.data.validation.Constraints;
导入play.data.validation.Constraints.Required;
导入play.db.jpa.jpa;
@映射超类
公共抽象类GenericDictionary扩展了泛型{
@必需的
公共字符串名称;
@必需的
公共布尔激活;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共布尔getActive(){
主动返回;
}
public void setActive(布尔状态){
this.active=stat;
}
/**
*选项。
*
*@归还地图
*/
公共地图获取选项(){
LinkedHashMap选项=新建LinkedHashMap();
对于(te:getList()){
GenericDictionary实体=(GenericDictionary)e;
options.put(例如id.toString(),entity.name);
}
返回选项;
}
//@覆盖
//公共列表getList(){
//return(List)JPA.em().createQuery(“FROM”+entityClass.getSimpleName()).getResultList();
// }
在()上公开作废{
this.active=true;
更新();
}
公众假期{
this.active=false;
更新();
}
}
它真的开始工作了。但是现在我在方法getOptions的GenericDictionary中出现了错误:for(te:getList()){options.put(e.id.toString(),e.name);}
错误是:app/models/GenericDictionary.java:45:错误:找不到符号[error]选项。put(e.id.toString(),e.name);[error]^[error]符号:变量名称[error]位置:类型为T[error]的变量e,其中T是类型变量:
public abstract class GenericDictionary<T extends Generic> extends Generic<T> {
Clinic.options()
new Clinic().getList()
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();
}
}