简体   繁体   English

ActiveJDBC 的虚拟属性 model

[英]Virtual attribute of ActiveJDBC model

I had a ActiveJDBC model called Job , and defined some static attributes like title , salary , workplace and so on.我有一个名为Job的 ActiveJDBC model,并定义了一些 static 属性,如titlesalaryworkplace等。

public class Job extends Model {

  public String getTitle() {
    return getString("title");
  }

  public void setTitle(String title) {
    setString("title", title);
  }

  public Integer getSalary() {
    return getInteger("salary");
  }

  public void setSalary(Integer salary) {
    setInteger("salary", salary);
  } 

  public String getWorkplace() {
    return getString("workplace");
  }

  public void setWorkplace(String workplace) {
    setString("workplace", workplace);
  }  
}

Now I want to find jobs based on geometry distance by below sql:现在我想根据 sql 以下的几何距离找到工作:

String sql = "select *, ST_distance(...) as distance from jobs... order by distance asc";
LazyList<Job> jobs = Job.findBySql(sql);

How can I read the virtual attribute distance from Job model?如何从Job model 读取虚拟属性distance

I have tried to add distance column in jobs table, and it reported error ERROR: ORDER BY "distance" is ambiguous我试图在jobs表中添加distance列,它报告错误ERROR: ORDER BY "distance" is ambiguous

Not sure what you mean by a "virtual" attribute, but ActiveJDBC models are just Java classes, so you can add whatever you want to them.不确定“虚拟”属性是什么意思,但 ActiveJDBC 模型只是 Java 类,因此您可以向其中添加任何内容。 JavaLite will not be handling them for you though. JavaLite 不会为您处理它们。 It is your responsibility to add appropriate setters and getters and maintain their state as if this was a regular Java class.您有责任添加适当的 setter 和 getter 并维护它们的 state,就像这是一个常规的 Java class 一样。

So, you want this wrapped into a model:所以,你想把它包装成一个 model:

String sql = "select *, ST_distance(...) as distance from jobs... order by distance asc";
LazyList<Job> jobs = Base.findAll(sql);

you are on the right path.你是在正确的道路上。 Please, read the JavaDoc for this method carefully:请仔细阅读此方法的 JavaDoc:

http://javalite.github.io/2.4-j8/org/javalite/activejdbc/Model.html#findBySQL-java.lang.String-java.lang.Object...- http://javalite.github.io/2.4-j8/org/javalite/activejdbc/Model.html#findBySQL-java.lang.String-java.lang.Object...-

Especially focus on this part: " Ensure that the query returns all columns associated with this model, so that the resulting models could hydrate themselves properly. ".特别关注这部分:“确保查询返回与此 model 关联的所有列,以便生成的模型可以适当地自我补充。 ”。

The "*" in your query takes care of the first part, and the second part will be ignored by a model automatically but will participate in the selection: " Returned columns that are not part of this model will be ignored, but can be used for clauses like above. "查询中的“*”处理第一部分,第二部分将自动被 model 忽略,但会参与选择:“不属于此 model 的返回列将被忽略,但可以使用对于像上面这样的条款。

So, all you have to do is to write a method like this:所以,你所要做的就是写一个这样的方法:


public class Job{

  public List<Map> getNearJobs(){

     String sql = "select *, ST_distance(. ? . ? .) as distance from jobs... order by distance asc";
     return Base.findAll(sql, getLogitude(), getLatitude());
  }

}

this way, you will construct a query that will bring (and order) the right maps with their attributes filled appropriately.这样,您将构建一个查询,该查询将带来(并排序)正确的地图,并适当地填充它们的属性。

So, you will get a list of Maps, not Jobs, but it will include all the data you need.因此,您将获得一个地图列表,而不是作业列表,但它将包含您需要的所有数据。 You can use this approach with some code acrobatics to create new models from a list of maps ( Model.fromMap() ) and separately inject your "distance" attribute in each model. However, I'm personally against this because then a model is not a model since a model is mapping to a table.您可以将这种方法与一些代码杂技结合使用,以从地图列表 ( Model.fromMap() ) 中创建新模型,并在每个 model 中单独注入“距离”属性。但是,我个人反对这种做法,因为那样的话 model 是不是 model,因为 model 正在映射到表。

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

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