当我的项目中没有任何实体时,如何将本机查询映射到POJO? [英] How to map a Native Query to a POJO, when I do not have any Entity on my project?

查看:197
本文介绍了当我的项目中没有任何实体时,如何将本机查询映射到POJO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个ETL过程从一个工具翻译成一个Java批处理API应用程序。在这个ETL过程中。在当前版本中(使用该工具),我们有许多SQL语句连接不同的表以生成所需的输出。转换到Java,JPA现在可用。



我想使用原生查询。这很好,因为它不需要为查询中使用的每个表创建实体,我可以使用POJO作为查询的结果(另外,我不需要重写查询)。阅读此答案我知道我可以使用 @SqlResultSetMapping 。问题是我的项目中没有任何实体,所以我不知道该注释的位置。 PS:在我的概念证明中,我目前手动从一个对象数组转换到POJO ,但我真的不喜欢这种方法。



在POJO中添加 @Entity 注释会导致我的应用程序不启动:
$ b


引起:org.hibernate.HibernateException:缺少表:MyTable

blockquote>

我不确定(现在正在搜索它),但我认为它可能是由我的persistence.xml中的此属性引起的。

 < property name =hibernate.hbm2ddl.autovalue =validate/> 


解决方案

其实我找到了 answer我在找



我可以在orm.xml中使用XML来定义 @SqlResultSetMapping 的行为,所以这个定义:

  @SqlResultSetMapping(
name =BookValueMapping,
classes = @ConstructorResult(
targetClass = BookValue.class,
columns = {
@ColumnResult(name =id,type = Long.class),
@ColumnResult(name =title),
@ColumnResult(name =version,type = Long blass),
@ColumnResult(name =authorName)}))

将以XML的形式定义如下:

 < sql-result-set-mapping name =BookValueMappingXml> 
< constructor-result target-class =org.thoughts.on.java.jpa.value.BookValue>
< column name =idclass =java.lang.Long/>
< column name =title/>
< column name =versionclass =java.lang.Long/>
< column name =authorName/>
< / constructor-result>
< / sql-result-set-mapping>

允许我在不需要实体的情况下定义它。


I am translating an ETL process from a tool to a Java batch API application. In this ETL process. In the current version (using the tool) we have many SQL statements that join different tables in order to generate the desired output. Translating to Java, JPA is now available.

I would like to use native queries. This would be nice because it would not require creating entities for every table used in the query and I could use POJOs for the result of the queries (also, I would not need to rewrite the queries). Reading this answer I know I could use @SqlResultSetMapping. The problem is that I do not have any entity in my project, so I do not know where to put this annotation. Is there anywhere I can put this annotation so the entity manager finds it?

PS: in my proof of concepts I am currently manually converting from an array of objects to the POJO, but I really don't like this approach.

Adding the @Entity annotation to the POJO will cause my application not to start:

Caused by: org.hibernate.HibernateException: Missing table: MyTable

I am not sure (searching for it right now), but I think it could be caused by this property in my persistence.xml

<property name="hibernate.hbm2ddl.auto" value="validate"/>

解决方案

Actually I found the answer I was looking for:

I can define @SqlResultSetMapping's behavior using XML in orm.xml, so this definition:

@SqlResultSetMapping(
        name = "BookValueMapping",
        classes = @ConstructorResult(
                targetClass = BookValue.class,
                columns = {
                    @ColumnResult(name = "id", type = Long.class),
                    @ColumnResult(name = "title"),
                    @ColumnResult(name = "version", type = Long.class),
                    @ColumnResult(name = "authorName")}))

Would be defined in XML like this:

<sql-result-set-mapping name="BookValueMappingXml">
    <constructor-result target-class="org.thoughts.on.java.jpa.value.BookValue">
        <column name="id" class="java.lang.Long"/>
        <column name="title"/>
        <column name="version" class="java.lang.Long"/>
        <column name="authorName"/>
    </constructor-result>
</sql-result-set-mapping>

Allowing me to do define it without needing an entity.

这篇关于当我的项目中没有任何实体时,如何将本机查询映射到POJO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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