简体   繁体   English

ActiveJDBC和Java泛型导致

[英]ActiveJDBC and Java Generics causing

I have the following two classes, where the Document extends an Abstract class that provides helper functions, one of which is a "find" method that builds queries to find records based on some simple logic. 我有以下两个类,其中Document扩展了一个提供帮助函数的Abstract类,其中一个是“ find”方法,该方法基于一些简单逻辑构建查询以查找记录。

public abstract class AbstractTable<T extends AbstractTable<T>> extends Model {
  ...
    public T find (String[] columns) {
        String whereClause = "";
        List<Object>    whereClauseData = new ArrayList<Object> ();

        for (String column : columns) {
            Object  data = this.get(column);
            if (data == null) {
                whereClause += column + " is null AND ";
            } else {
                whereClause += column + " = ? AND ";
                whereClauseData.add (data);
            }
        }

        return findFirst (whereClause.substring(0, whereClause.length () - 5), whereClauseData.toArray());
    }
}


public class Document extends AbstractTable<Document> { 
...
    public Document findExistingObject(Document document) {
        String[] columns = new String[] {"court_case_id", "number", "name", "file_date"};
        return super.find (columns);
    }
}

When I run this code, and the "findExistingObject" method is called on a Document, I receive this exception: 当我运行此代码,并在Document上调用“ findExistingObject”方法时,我收到此异常:

Exception in thread "main" org.javalite.activejdbc.InitException: failed to determine Model class name, are you sure models have been instrumented? 线程“主”中的异常org.javalite.activejdbc.InitException:无法确定模型类名称,您确定已经对模型进行了检测吗?

I've made completely sure that I've instrumented the classes. 我已经完全确定我已经对这些类进行了检测。 When I move the code from AbstractTable into Document, everything works perfectly. 当我将代码从AbstractTable移到Document时,一切工作正常。 I'm hoping someone can lend some advice, or help, that might show me what I'm doing wrong. 我希望有人可以提供一些建议或帮助,这些建议或帮助可以告诉我我做错了什么。

Thanks in advance. 提前致谢。

The exact reason for your issue is not generics, but instrumentation. 问题的确切原因不是泛型,而是检测。 Instrumentation skips abstract models, which means that the method findFirst is called on class Model , and not on Document . 检测跳过抽象模型,这意味着在类Model而不是Document上调用findFirst方法。 You need to invoke a method findFirst on the model Document . 您需要在模型Document上调用方法findFirst Here is a version of code that will work for you: 这是适合您的代码版本:

public T find (String[] columns) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String whereClause = "";
    List<Object> whereClauseData = new ArrayList<>();

    for (String column : columns) {
        Object  data = get(column);
        if (data == null) {
            whereClause += column + " is null AND ";
        } else {
            whereClause += column + " = ? AND ";
            whereClauseData.add (data);
        }
    }

    Method findFirst = getClass().getDeclaredMethod("findFirst", String.class, Object[].class);
    return (T) findFirst.invoke(null, whereClause.substring(0, whereClause.length () - 5), whereClauseData.toArray());
}

There is a bit of ugliness there, but at least you can apply this across all your models (if this is what you want). 那里有些丑陋,但至少您可以将其应用于所有模型(如果这是您想要的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM