嘿,根据那篇文章:link我已经建立了模型继承结构。

但是我想扩展它,添加两个通用模型,这些模型将扩展主要的GenericModel并添加一些参数和功能。这些类将称为GenericDictionary和GenericAudit。这些类将由实体扩展。

这是我的新结构:
GenericModel(包含ID,创建日期)

public abstract class Generic<T extends Generic> {


-GenericDictionary(包含GenericModel并添加名称参数和getter / setter)

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


以及扩展GenericDictionary的类:

public class InconsistencyOrigin extends GenericDictionary<InconsistencyOrigin> {


-GenericAudit(包含GenericModel并添加其他参数-dosnt包含名称!!!)

当我尝试执行此操作时,出现错误:

 error: type argument InconsistencyOrigin is not within bounds of type-variable T


我的课再一次:

package models;

import java.lang.reflect.ParameterizedType;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.NoResultException;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import controllers.common.Index;
import play.Logger;
import play.data.validation.Constraints;
import play.db.jpa.JPA;

@MappedSuperclass
public abstract class Generic<T extends Generic> {
    @Transient
    private 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> 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);
    }
}




package models;

import play.data.validation.Constraints;

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

    @Constraints.Required
    public String name;

    public String getName() {
        return name;
    }

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

}


package models;

import javax.persistence.Entity;
import javax.persistence.Table;

import play.data.validation.Constraints;

@Entity
@Table(name="intranet___InconsistencyOrigin")
public class InconsistencyOrigin extends GenericDictionary<InconsistencyOrigin> {

}

最佳答案

您将T指定为extends Generic并为T提供InconsistencyOrigin,因此InconsistencyOrigin必须扩展Generic,而不必扩展Generic。

09-06 12:56