阅读背景:

将RawSql结果映射到Java Ebean中的对象

来源:互联网 

Let's say I have a class Article which is automatically mapped by Java Ebean as a database table.

假设我有一个类文章,它由Java Ebean自动映射为数据库表。

For this table I wanted to retrieve entries via a RawSql query, because I find SQL more simple when it gets to complex queries with many joins and such. By far I have managed to give my SQL statement to the Parser. The query is correct, I already checked that.

对于这个表,我想通过RawSql查询检索条目,因为当我遇到具有许多连接等的复杂查询时,我发现SQL更简单。到目前为止,我已经设法将我的SQL语句提供给Parser。查询是正确的,我已经检查过了。

The only problem is, that I don't know, how to map the database results to my Article class. I know, that there is a columnMapping(...) method but honestly, I am to lazy to map every single column manually...

唯一的问题是,我不知道,如何将数据库结果映射到我的Article类。我知道,有一个columnMapping(...)方法但老实说,我懒得手动映射每一列...

Isn't there another way to just like myResults.mapToClass(Article.class) to retrieve something like a List<Article>?

是否有另一种方法可以像myResults.mapToClass(Article.class)一样检索List

之类的东西?

This is the code I already have:

这是我已有的代码:

Finder<String, Article> find = new Finder<String, Article>(
        String.class, Article.class);
String sql = "SELECT * FROM article [...]";
RawSql rawSql = RawSqlBuilder.parse(sql).create();
List<Article> returnList = find.setRawSql(rawSql).findList();

Alternatively:

Finder<String, Article> find = new Finder<String, Article>(
                String.class, Article.class);
String sql = "SELECT id, title, sub_title FROM article [...]";
RawSql rawSql = RawSqlBuilder.parse(sql)
            .columnMapping("id", "id")
            .columnMapping("title", "title")
            .columnMapping("sub_title", "subTitle")
            .create();
List<Article> resultList = find.setRawSql(rawSql).findList();

1 个解决方案

#1


0  

A lot of happened in Ebean since the question was asked, but I think the problem is still valid. the new RawSqlBuilder.tableMapping() makes things easier as can be seen in the code below, but afaik it still needs manual mapping of all attributes (no SELECT table.* FROM table)

自问题问题以来,Ebean发生了很多事情,但我认为这个问题仍然有效。新的RawSqlBuilder.tableMapping()使事情变得更容易,如下面的代码所示,但是afaik它仍然需要手动映射所有属性(没有SELECT表。* FROM表)

I have this exact problem, and worked around this by creating a helper object (@Entity/@Sql) that I map to. E.g. CustomerWithPurchaseStats.

我有这个确切的问题,并通过创建我映射到的辅助对象(@ Entity / @ Sql)来解决这个问题。例如。 CustomerWithPurchaseStats。

Extract:

@Entity
@Sql
public class CustomerWithPurchaseStats {

    @OneToOne
    private Customer customer;
...

And in the DAO:

在DAO中:

public List<CustomerWithPurchaseStats> getAllCustomersWithPurchaseStats() {
    StringBuilder sql = new StringBuilder("SELECT cu.id, <manually add all fields you need mapped ").append(" FROM customer cu ");
    RawSqlBuilder rawSqlBuilder = RawSqlBuilder.parse(sql.toString());
    rawSqlBuilder.tableAliasMapping("cu", "customer").create();
    return Ebean.find(CustomerWithPurchaseStats.class)
                .setRawSql(rawSqlBuilder.create())
                .findList();
}

分享到: