没有基础表的Hibernate实体 [英] Hibernate entities without underlying tables

查看:111
本文介绍了没有基础表的Hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常难看的遗留数据库系统,我需要与之集成。基本上我在系统上做一些只读报告,我不想设置代表我正在处理的每个表的一千个实体。相反,我想为我生成的每个报告类型定义一个实体(实质上是来自不同表的一堆列的联合),然后让hibernate映射来讨厌(许多已加入,很多联合) sql查询到这样的实体列表。

I have a really ugly legacy database system that I need to integrate with. Essentially I'm doing some read only reporting on the system, and I don't want to set up a thousand entities representing each of the tables that I'm working on. Instead, I'd like to just define an Entity for each of the report-types that I generate (essentially a union of a bunch of columns from different tables), and then let hibernate map from the nasty (many joined, many unioned) sql query to a list of such entities.

问题是:我可以创建一个没有基础表的实体,并使用sql语句填充一个所述实体的清单?

The question is: can I create an entity that doesn't have an underlying table, and use a sql statement to populate a list of said entities?

谢谢!

推荐答案

我们这样做一直都是这样 - 以下是我们如何做的事情:

We do that sort of thing all the time - and here is how we do it:


  1. 定义一个类似bean的简单对象来表示每个报告中的输出行:

  1. Define a simple bean-like object to represent each row of output in your report:

public class CityStateRevenueReport {

    private String mId;
    private String mState;
    private String mCity;
    private Double mRevenue;

    public String getId() { return mId; }
    public void setId(String i) { mId = i; }
    public String getState() { return mState; }
    public void setState(String s) { mState = s; }
    public String getCity() { return mCity; }
    public void setCity(String c) { mCity = c; }
    public Double getReveneue() { return mRevenue; }
    public void setRevneue(Double d) { mRevenue = d; }
}


  • 定义一个hibernate映射文件,CityStateRevneueReport.hbm.xml:

  • Define a hibernate mapping file, CityStateRevneueReport.hbm.xml:

    <?xml version="1.0" ?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;>
    <hibernate-mapping>
        <class entity-name="CityStateRevenueReport">
            <id name="Id" type="java.lang.String" column="report_id">
                <generator class="sequence" />
            </id>
            <property name="city" type="string" column="city" />
            <property name="state" type="string" column="state" />
            <property name="revenue" type="double" column="revenue" />
        </class>
        <sql-query name="runReport">
            <![CDATA[ 
            SELECT {r.*} FROM 
                (select some_id_value as report_id, 
                        state_abbreviation as state, 
                        city_name as city, 
                        dollar_amount as revenue 
                   from -- tables, joins, other SQL insanity 
                 ) r
            ]]>
            <return alias="r" class="CityStateRevenueReport" />
        </sql-query>
    </hibernate-mapping>
    


  • 然后运行查询并填充实例:

  • Then run the query and populate instances:

    public List<CityStateRevenueReport> runReport() {
    
        List<CityStateRevenueReport> reports = 
                                 new ArrayList<CityStateRevenueReport>();
        List<HashMap> maps = session.getNamedQuery("runReport").list()
        for ( HashMap map : results ) {
            CityStateRevenueReport report = new CityStateRevenueReport();
            report.setState(map.get("state"));
            report.setCity(map.get("city"));
            report.setRevenue(Double.parseDouble(map.get("revenue"));
            reports.add(report);
        }
        return reports;
    }
    


  • 这篇关于没有基础表的Hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆